From 83cad3625b98920c8aaebd68891231d590dea1ca Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Mon, 26 Dec 2022 15:53:15 +0100 Subject: [PATCH 1/4] Remove unused setTerminalEcho() It had been replaced with readpassphrase in #5092 Signed-off-by: Azat Khuzhin --- base/base/CMakeLists.txt | 1 - base/base/setTerminalEcho.cpp | 28 ---------------------------- base/base/setTerminalEcho.h | 4 ---- src/Client/ConnectionParameters.cpp | 1 - 4 files changed, 34 deletions(-) delete mode 100644 base/base/setTerminalEcho.cpp delete mode 100644 base/base/setTerminalEcho.h diff --git a/base/base/CMakeLists.txt b/base/base/CMakeLists.txt index d788bd6f092..4a65240f4f0 100644 --- a/base/base/CMakeLists.txt +++ b/base/base/CMakeLists.txt @@ -12,7 +12,6 @@ set (SRCS mremap.cpp phdr_cache.cpp preciseExp10.cpp - setTerminalEcho.cpp shift10.cpp sleep.cpp terminalColors.cpp diff --git a/base/base/setTerminalEcho.cpp b/base/base/setTerminalEcho.cpp deleted file mode 100644 index 759dca19119..00000000000 --- a/base/base/setTerminalEcho.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - - -void setTerminalEcho(bool enable) -{ - /// Obtain terminal attributes, - /// toggle the ECHO flag - /// and set them back. - - struct termios tty{}; - - if (0 != tcgetattr(STDIN_FILENO, &tty)) - throw std::runtime_error(std::string("setTerminalEcho failed get: ") + errnoToString()); - - if (enable) - tty.c_lflag |= ECHO; - else - tty.c_lflag &= ~ECHO; - - if (0 != tcsetattr(STDIN_FILENO, TCSANOW, &tty)) - throw std::runtime_error(std::string("setTerminalEcho failed set: ") + errnoToString()); -} diff --git a/base/base/setTerminalEcho.h b/base/base/setTerminalEcho.h deleted file mode 100644 index 98e8f5a87e3..00000000000 --- a/base/base/setTerminalEcho.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -/// Enable or disable echoing of typed characters. Throws std::runtime_error on error. -void setTerminalEcho(bool enable); diff --git a/src/Client/ConnectionParameters.cpp b/src/Client/ConnectionParameters.cpp index 6d538fee307..1a0922b2133 100644 --- a/src/Client/ConnectionParameters.cpp +++ b/src/Client/ConnectionParameters.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include From b7f92454bf62fb8c5a60aded76819655308804e1 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sat, 10 Dec 2022 11:16:31 +0100 Subject: [PATCH 2/4] Move LineReader/ReplxxLineReader out from base This will simplify linking dependencies, and anyway this classes are not base in any sense. Signed-off-by: Azat Khuzhin --- base/base/CMakeLists.txt | 7 --- programs/client/CMakeLists.txt | 4 ++ programs/local/CMakeLists.txt | 4 ++ src/CMakeLists.txt | 11 ++++ src/Client/CMakeLists.txt | 2 +- src/Client/ClientBase.cpp | 54 +++++++++---------- src/Client/ClientBaseHelpers.h | 2 +- {base/base => src/Client}/LineReader.cpp | 7 ++- {base/base => src/Client}/LineReader.h | 5 ++ .../base => src/Client}/ReplxxLineReader.cpp | 7 ++- {base/base => src/Client}/ReplxxLineReader.h | 6 ++- src/Client/Suggest.h | 2 +- utils/zookeeper-cli/CMakeLists.txt | 4 +- utils/zookeeper-cli/zookeeper-cli.cpp | 4 +- 14 files changed, 75 insertions(+), 44 deletions(-) rename {base/base => src/Client}/LineReader.cpp (99%) rename {base/base => src/Client}/LineReader.h (99%) rename {base/base => src/Client}/ReplxxLineReader.cpp (99%) rename {base/base => src/Client}/ReplxxLineReader.h (98%) diff --git a/base/base/CMakeLists.txt b/base/base/CMakeLists.txt index 4a65240f4f0..79e62586ad4 100644 --- a/base/base/CMakeLists.txt +++ b/base/base/CMakeLists.txt @@ -8,7 +8,6 @@ set (SRCS getPageSize.cpp getThreadId.cpp JSON.cpp - LineReader.cpp mremap.cpp phdr_cache.cpp preciseExp10.cpp @@ -16,7 +15,6 @@ set (SRCS sleep.cpp terminalColors.cpp errnoToString.cpp - ReplxxLineReader.cpp StringRef.cpp safeExit.cpp throwError.cpp @@ -39,11 +37,6 @@ else () target_compile_definitions(common PUBLIC WITH_COVERAGE=0) endif () -# FIXME: move libraries for line reading out from base -if (TARGET ch_rust::skim) - target_link_libraries(common PUBLIC ch_rust::skim) -endif() - target_include_directories(common PUBLIC .. "${CMAKE_CURRENT_BINARY_DIR}/..") if (OS_DARWIN AND NOT USE_STATIC_LIBRARIES) diff --git a/programs/client/CMakeLists.txt b/programs/client/CMakeLists.txt index d212da59908..e160355ef7b 100644 --- a/programs/client/CMakeLists.txt +++ b/programs/client/CMakeLists.txt @@ -13,6 +13,10 @@ set (CLICKHOUSE_CLIENT_LINK string_utils ) +if (TARGET ch_rust::skim) + list(APPEND CLICKHOUSE_CLIENT_LINK PRIVATE ch_rust::skim) +endif() + # Always use internal readpassphrase list(APPEND CLICKHOUSE_CLIENT_LINK PRIVATE readpassphrase) diff --git a/programs/local/CMakeLists.txt b/programs/local/CMakeLists.txt index ad4406156c4..7366f8685a4 100644 --- a/programs/local/CMakeLists.txt +++ b/programs/local/CMakeLists.txt @@ -18,6 +18,10 @@ if(NOT CLICKHOUSE_ONE_SHARED) target_link_libraries(clickhouse-local-lib PRIVATE clickhouse-server-lib) endif() +if (TARGET ch_rust::skim) + target_link_libraries(clickhouse-local-lib PRIVATE ch_rust::skim) +endif() + # Always use internal readpassphrase target_link_libraries(clickhouse-local-lib PRIVATE readpassphrase) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7e3364dc92e..484924a51bd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -332,6 +332,12 @@ macro (dbms_target_link_libraries) endforeach () endmacro () +macro (dbms_target_include_directories) + foreach (module ${all_modules}) + target_include_directories (${module} ${ARGN}) + endforeach () +endmacro () + dbms_target_include_directories (PUBLIC "${ClickHouse_SOURCE_DIR}/src" "${ClickHouse_BINARY_DIR}/src") target_include_directories (clickhouse_common_io PUBLIC "${ClickHouse_SOURCE_DIR}/src" "${ClickHouse_BINARY_DIR}/src") @@ -589,6 +595,11 @@ if (TARGET ch_contrib::annoy) dbms_target_link_libraries(PUBLIC ch_contrib::annoy) endif() +if (TARGET ch_rust::skim) + # Add only -I, library is needed only for clickhouse-client/clickhouse-local + dbms_target_include_directories(PRIVATE $) +endif() + include ("${ClickHouse_SOURCE_DIR}/cmake/add_check.cmake") if (ENABLE_TESTS) diff --git a/src/Client/CMakeLists.txt b/src/Client/CMakeLists.txt index 119414a8a70..83bbe418246 100644 --- a/src/Client/CMakeLists.txt +++ b/src/Client/CMakeLists.txt @@ -1,3 +1,3 @@ if (ENABLE_EXAMPLES) add_subdirectory(examples) -endif() \ No newline at end of file +endif() diff --git a/src/Client/ClientBase.cpp b/src/Client/ClientBase.cpp index 09e44a3ac09..2e1290a2c4e 100644 --- a/src/Client/ClientBase.cpp +++ b/src/Client/ClientBase.cpp @@ -1,42 +1,30 @@ #include +#include +#include +#include +#include +#include -#include -#include -#include -#include - -#include "config.h" - +#include +#include +#include +#include #include #include -#include -#include #include -#include #include #include #include #include -#include -#include -#include -#include -#include -#include - -#include "config_version.h" - #include #include #include #include #include #include -#include - -#include -#include -#include "TestTags.h" +#include +#include +#include #include #include @@ -53,26 +41,36 @@ #include #include #include +#include #include #include #include -#include -#include #include #include #include #include #include +#include +#include #include #include #include #include #include -#include #include -#include + +#include +#include + #include +#include +#include +#include +#include + +#include "config_version.h" +#include "config.h" namespace fs = std::filesystem; diff --git a/src/Client/ClientBaseHelpers.h b/src/Client/ClientBaseHelpers.h index 2a79332eb98..adc1c81b3c5 100644 --- a/src/Client/ClientBaseHelpers.h +++ b/src/Client/ClientBaseHelpers.h @@ -4,7 +4,7 @@ #include "config.h" #if USE_REPLXX -# include +# include #endif diff --git a/base/base/LineReader.cpp b/src/Client/LineReader.cpp similarity index 99% rename from base/base/LineReader.cpp rename to src/Client/LineReader.cpp index cc632f79638..f49e48be617 100644 --- a/base/base/LineReader.cpp +++ b/src/Client/LineReader.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -65,6 +65,9 @@ void addNewWords(Words & to, const Words & from, Compare comp) } +namespace DB +{ + replxx::Replxx::completions_t LineReader::Suggest::getCompletions(const String & prefix, size_t prefix_length) { std::string_view last_word; @@ -202,3 +205,5 @@ LineReader::InputStatus LineReader::readOneLine(const String & prompt) trim(input); return INPUT_LINE; } + +} diff --git a/base/base/LineReader.h b/src/Client/LineReader.h similarity index 99% rename from base/base/LineReader.h rename to src/Client/LineReader.h index d4ab327fe00..321cf41b77e 100644 --- a/base/base/LineReader.h +++ b/src/Client/LineReader.h @@ -9,6 +9,9 @@ #include #include +namespace DB +{ + class LineReader { public: @@ -68,3 +71,5 @@ protected: virtual InputStatus readOneLine(const String & prompt); virtual void addToHistory(const String &) {} }; + +} diff --git a/base/base/ReplxxLineReader.cpp b/src/Client/ReplxxLineReader.cpp similarity index 99% rename from base/base/ReplxxLineReader.cpp rename to src/Client/ReplxxLineReader.cpp index 261f0e2a16a..ac3f6fcbe53 100644 --- a/base/base/ReplxxLineReader.cpp +++ b/src/Client/ReplxxLineReader.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -269,6 +269,9 @@ void convertHistoryFile(const std::string & path, replxx::Replxx & rx) } +namespace DB +{ + static bool replxx_last_is_delimiter = false; void ReplxxLineReader::setLastIsDelimiter(bool flag) { @@ -508,3 +511,5 @@ void ReplxxLineReader::enableBracketedPaste() bracketed_paste_enabled = true; rx.enable_bracketed_paste(); } + +} diff --git a/base/base/ReplxxLineReader.h b/src/Client/ReplxxLineReader.h similarity index 98% rename from base/base/ReplxxLineReader.h rename to src/Client/ReplxxLineReader.h index 428fbf144c3..d36a1d0f42c 100644 --- a/base/base/ReplxxLineReader.h +++ b/src/Client/ReplxxLineReader.h @@ -1,9 +1,11 @@ #pragma once #include "LineReader.h" - #include +namespace DB +{ + class ReplxxLineReader : public LineReader { public: @@ -36,3 +38,5 @@ private: std::string editor; }; + +} diff --git a/src/Client/Suggest.h b/src/Client/Suggest.h index 25d45f7ffaf..cfe9315879c 100644 --- a/src/Client/Suggest.h +++ b/src/Client/Suggest.h @@ -5,8 +5,8 @@ #include #include #include +#include #include -#include #include diff --git a/utils/zookeeper-cli/CMakeLists.txt b/utils/zookeeper-cli/CMakeLists.txt index edccb69755e..be8cf81320c 100644 --- a/utils/zookeeper-cli/CMakeLists.txt +++ b/utils/zookeeper-cli/CMakeLists.txt @@ -1,2 +1,4 @@ -clickhouse_add_executable(clickhouse-zookeeper-cli zookeeper-cli.cpp) +clickhouse_add_executable(clickhouse-zookeeper-cli + zookeeper-cli.cpp + ${ClickHouse_SOURCE_DIR}/src/Client/LineReader.cpp) target_link_libraries(clickhouse-zookeeper-cli PRIVATE clickhouse_common_zookeeper_no_log) diff --git a/utils/zookeeper-cli/zookeeper-cli.cpp b/utils/zookeeper-cli/zookeeper-cli.cpp index bfcdb0a90de..fe11c66ea9c 100644 --- a/utils/zookeeper-cli/zookeeper-cli.cpp +++ b/utils/zookeeper-cli/zookeeper-cli.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include @@ -70,7 +70,7 @@ int main(int argc, char ** argv) Poco::Logger::root().setLevel("trace"); zkutil::ZooKeeper zk{zkutil::ZooKeeperArgs(argv[1])}; - LineReader lr({}, false, {"\\"}, {}); + DB::LineReader lr({}, false, {"\\"}, {}); do { From 1f9a6076b9151ed830fc4ab24f04bbdf9a923969 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Mon, 26 Dec 2022 16:04:26 +0100 Subject: [PATCH 3/4] Use DB::ReadBufferFromFile over std::ifstream in ReplxxLineReader Signed-off-by: Azat Khuzhin --- src/Client/ReplxxLineReader.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Client/ReplxxLineReader.cpp b/src/Client/ReplxxLineReader.cpp index ac3f6fcbe53..9fc0fec761d 100644 --- a/src/Client/ReplxxLineReader.cpp +++ b/src/Client/ReplxxLineReader.cpp @@ -1,6 +1,10 @@ #include #include +#include +#include +#include + #include #include #include @@ -108,13 +112,11 @@ void writeRetry(int fd, const std::string & data) } std::string readFile(const std::string & path) { - std::ifstream t(path); - std::string str; - t.seekg(0, std::ios::end); - str.reserve(t.tellg()); - t.seekg(0, std::ios::beg); - str.assign((std::istreambuf_iterator(t)), std::istreambuf_iterator()); - return str; + std::string out; + DB::WriteBufferFromString out_buffer(out); + DB::ReadBufferFromFile in_buffer(path); + DB::copyData(in_buffer, out_buffer); + return out; } /// Simple wrapper for temporary files. From e719fd218d4673f6011c024b7d3c69709bb0c5b2 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Mon, 26 Dec 2022 16:42:40 +0100 Subject: [PATCH 4/4] Add excludes for std::cout usage in LineReader Signed-off-by: Azat Khuzhin --- utils/check-style/check-style | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/check-style/check-style b/utils/check-style/check-style index 5e5631e7e58..3db014a4026 100755 --- a/utils/check-style/check-style +++ b/utils/check-style/check-style @@ -292,6 +292,7 @@ std_cerr_cout_excludes=( # IProcessor::dump() src/Processors/IProcessor.cpp src/Client/ClientBase.cpp + src/Client/LineReader.cpp src/Client/QueryFuzzer.cpp src/Client/Suggest.cpp src/Bridge/IBridge.cpp