From f4335d4a7ebce70a975140dab22d1db295958572 Mon Sep 17 00:00:00 2001 From: Frank Celler Date: Thu, 10 Mar 2016 21:56:29 +0000 Subject: [PATCH 1/4] fixed color output --- lib/ApplicationFeatures/ConsoleFeature.cpp | 29 +++++++++++++--------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/ApplicationFeatures/ConsoleFeature.cpp b/lib/ApplicationFeatures/ConsoleFeature.cpp index 83e0bf5eb4..f5b464def2 100644 --- a/lib/ApplicationFeatures/ConsoleFeature.cpp +++ b/lib/ApplicationFeatures/ConsoleFeature.cpp @@ -98,8 +98,8 @@ void ConsoleFeature::collectOptions(std::shared_ptr options) { new StringParameter(&_prompt)); #if _WIN32 - options->addOption("--console.code-page", "Windows code page to use", - new Int16Parameter(&_codePage)); + options->addHiddenOption("--console.code-page", "Windows code page to use", + new Int16Parameter(&_codePage)); #endif } @@ -137,17 +137,17 @@ int CONSOLE_COLOR = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE; static void _print2(std::string const& s) { size_t sLen = s.size(); + + if (sLen == 0) { + return; + } + LPWSTR wBuf = new WCHAR[sLen + 1]; - int wLen = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, wBuf, + int wLen = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), (int)sLen, wBuf, (int)((sizeof WCHAR) * (sLen + 1))); if (wLen) { auto handle = GetStdHandle(STD_OUTPUT_HANDLE); - - CONSOLE_SCREEN_BUFFER_INFO bufferInfo; - GetConsoleScreenBufferInfo(handle, &bufferInfo); - - SetConsoleTextAttribute(handle, CONSOLE_ATTRIBUTE | CONSOLE_COLOR); DWORD n; @@ -253,22 +253,27 @@ static void _print(std::string const& s) { } - _print2(line.substr(pos)); + if (line.size() > pos) { + _print2(line.substr(pos)); + } } } + + auto handle = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleTextAttribute(handle, CONSOLE_ATTRIBUTE | CONSOLE_COLOR); } #endif // prints a string to stdout, without a newline void ConsoleFeature::printContinuous(std::string const& s) { -#ifdef _WIN32 - // no, we cannot use std::cout as this doesn't support UTF-8 on Windows - if (s.empty()) { return; } +#ifdef _WIN32 + // no, we cannot use std::cout as this doesn't support UTF-8 on Windows + if (!_cygwinShell) { // no, we cannot use std::cout as this doesn't support UTF-8 on Windows // fprintf(stdout, "%s\r\n", s.c_str()); From ab6224e2aec99de6dcad7de878a992e5d5149bab Mon Sep 17 00:00:00 2001 From: Frank Celler Date: Thu, 10 Mar 2016 23:02:59 +0100 Subject: [PATCH 2/4] cleanup --- lib/ApplicationFeatures/ConsoleFeature.cpp | 125 ++++++++++----------- 1 file changed, 57 insertions(+), 68 deletions(-) diff --git a/lib/ApplicationFeatures/ConsoleFeature.cpp b/lib/ApplicationFeatures/ConsoleFeature.cpp index f5b464def2..051c64d820 100644 --- a/lib/ApplicationFeatures/ConsoleFeature.cpp +++ b/lib/ApplicationFeatures/ConsoleFeature.cpp @@ -99,7 +99,7 @@ void ConsoleFeature::collectOptions(std::shared_ptr options) { #if _WIN32 options->addHiddenOption("--console.code-page", "Windows code page to use", - new Int16Parameter(&_codePage)); + new Int16Parameter(&_codePage)); #endif } @@ -141,7 +141,7 @@ static void _print2(std::string const& s) { if (sLen == 0) { return; } - + LPWSTR wBuf = new WCHAR[sLen + 1]; int wLen = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), (int)sLen, wBuf, (int)((sizeof WCHAR) * (sLen + 1))); @@ -151,7 +151,7 @@ static void _print2(std::string const& s) { SetConsoleTextAttribute(handle, CONSOLE_ATTRIBUTE | CONSOLE_COLOR); DWORD n; - WriteConsoleW(handle, wBuf, (DWORD) wLen, &n, NULL); + WriteConsoleW(handle, wBuf, (DWORD)wLen, &n, NULL); } else { fprintf(stdout, "window error: '%d' \r\n", GetLastError()); fprintf(stdout, "%s\r\n", s.c_str()); @@ -162,17 +162,14 @@ static void _print2(std::string const& s) { } } -static void _newLine() { - fprintf(stdout, "\n"); -} +static void _newLine() { fprintf(stdout, "\n"); } static void _print(std::string const& s) { auto pos = s.find_first_of("\x1b"); if (pos == std::string::npos) { _print2(s); - } - else { + } else { std::vector lines = StringUtils::split(s, '\x1b', '\0'); int i = 0; @@ -181,80 +178,81 @@ static void _print(std::string const& s) { size_t pos = 0; if (i++ != 0 && !line.empty()) { - char c = line[0]; - + char c = line[0]; + if (c == '[') { - int code = 0; - - for (++pos; pos < line.size(); ++pos) { + int code = 0; + + for (++pos; pos < line.size(); ++pos) { c = line[pos]; - if ('0' <= c && c <= '9') { - code = code * 10 + (c - '0'); - } - else if (c == 'm' || c == ';') { - switch (code) { - case 0: - CONSOLE_ATTRIBUTE = 0; - CONSOLE_COLOR = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE; - break; - - case 1: // BOLD - case 5: // BLINK - CONSOLE_ATTRIBUTE = FOREGROUND_INTENSITY; - break; - - case 30: - CONSOLE_COLOR = BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN; - break; + if ('0' <= c && c <= '9') { + code = code * 10 + (c - '0'); + } else if (c == 'm' || c == ';') { + switch (code) { + case 0: + CONSOLE_ATTRIBUTE = 0; + CONSOLE_COLOR = + FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE; + break; - case 31: - CONSOLE_COLOR = FOREGROUND_RED; - break; + case 1: // BOLD + case 5: // BLINK + CONSOLE_ATTRIBUTE = FOREGROUND_INTENSITY; + break; + + case 30: + CONSOLE_COLOR = + BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN; + break; + + case 31: + CONSOLE_COLOR = FOREGROUND_RED; + break; case 32: - CONSOLE_COLOR = FOREGROUND_GREEN; - break; + CONSOLE_COLOR = FOREGROUND_GREEN; + break; case 33: - CONSOLE_COLOR = FOREGROUND_RED | FOREGROUND_GREEN; - break; + CONSOLE_COLOR = FOREGROUND_RED | FOREGROUND_GREEN; + break; case 34: - CONSOLE_COLOR = FOREGROUND_BLUE; - break; + CONSOLE_COLOR = FOREGROUND_BLUE; + break; case 35: - CONSOLE_COLOR = FOREGROUND_BLUE | FOREGROUND_RED; - break; + CONSOLE_COLOR = FOREGROUND_BLUE | FOREGROUND_RED; + break; case 36: - CONSOLE_COLOR = FOREGROUND_BLUE | FOREGROUND_GREEN; - break; + CONSOLE_COLOR = FOREGROUND_BLUE | FOREGROUND_GREEN; + break; case 37: - CONSOLE_COLOR = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE; - break; + CONSOLE_COLOR = + FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE; + break; case 39: - CONSOLE_COLOR = 0; - break; + CONSOLE_COLOR = 0; + break; } - code = 0; + code = 0; } - if (c == 'm') { - ++pos; - break; - } - } + if (c == 'm') { + ++pos; + break; + } + } } } - if (line.size() > pos) { - _print2(line.substr(pos)); + _print2(line.substr(pos)); } } } @@ -262,7 +260,7 @@ static void _print(std::string const& s) { auto handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(handle, CONSOLE_ATTRIBUTE | CONSOLE_COLOR); } - + #endif // prints a string to stdout, without a newline @@ -307,7 +305,6 @@ void ConsoleFeature::printLine(std::string const& s) { } if (true) { - std::vector lines = StringUtils::split(s, '\n', '\0'); for (auto& line : lines) { @@ -322,9 +319,7 @@ void ConsoleFeature::printLine(std::string const& s) { } } -void ConsoleFeature::printErrorLine(std::string const& s) { - printLine(s); -} +void ConsoleFeature::printErrorLine(std::string const& s) { printLine(s); } std::string ConsoleFeature::readPassword(std::string const& message) { std::string password; @@ -391,13 +386,7 @@ static std::string StripBinary(std::string const& value) { void ConsoleFeature::print(std::string const& message) { if (_toPager == stdout) { -#ifdef _WIN32 - // at moment the formating is ignored in windows - printLine(message); -#else - fprintf(_toPager, "%s", message.c_str()); -#endif - + printContinuous(message); } else { std::string sanitized = StripBinary(message.c_str()); fprintf(_toPager, "%s", sanitized.c_str()); From ddc536994984cb5e7d319e6a25993541b640d7e6 Mon Sep 17 00:00:00 2001 From: Wilfried Goesgens Date: Thu, 10 Mar 2016 23:26:36 +0100 Subject: [PATCH 3/4] test for OPENSSL_NO_SSL3_METHOD; this fixes #1773 --- lib/Basics/ssl-helper.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/Basics/ssl-helper.cpp b/lib/Basics/ssl-helper.cpp index f9a107db06..499d1e707e 100644 --- a/lib/Basics/ssl-helper.cpp +++ b/lib/Basics/ssl-helper.cpp @@ -28,7 +28,9 @@ using namespace arangodb; +#ifndef OPENSSL_NO_SSL3_METHOD extern "C" const SSL_METHOD* SSLv3_method(void); +#endif //////////////////////////////////////////////////////////////////////////////// /// @brief creates an SSL context @@ -44,10 +46,11 @@ SSL_CTX* arangodb::sslContext(protocol_e protocol, std::string const& keyfile) { meth = SSLv2_method(); break; #endif +#ifndef OPENSSL_NO_SSL3_METHOD case SSL_V3: meth = SSLv3_method(); break; - +#endif case SSL_V23: meth = SSLv23_method(); break; From 46b702b2cbdd184a767cc034d749cc35470938f6 Mon Sep 17 00:00:00 2001 From: Frank Celler Date: Thu, 10 Mar 2016 22:59:51 +0000 Subject: [PATCH 4/4] fixed colors --- lib/ApplicationFeatures/ConsoleFeature.cpp | 58 ++++++++++++---------- lib/ApplicationFeatures/ConsoleFeature.h | 13 +++++ lib/Basics/shell-colors.h | 2 +- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/lib/ApplicationFeatures/ConsoleFeature.cpp b/lib/ApplicationFeatures/ConsoleFeature.cpp index 051c64d820..b5005e6659 100644 --- a/lib/ApplicationFeatures/ConsoleFeature.cpp +++ b/lib/ApplicationFeatures/ConsoleFeature.cpp @@ -35,6 +35,11 @@ using namespace arangodb; using namespace arangodb::basics; using namespace arangodb::options; +#ifdef _WIN32 +static const int FOREGROUND_WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; +static const int BACKGROUND_WHITE = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE; +#endif + ConsoleFeature::ConsoleFeature(application_features::ApplicationServer* server) : ApplicationFeature(server, "ConsoleFeature"), #ifdef _WIN32 @@ -63,6 +68,17 @@ ConsoleFeature::ConsoleFeature(application_features::ApplicationServer* server) #if _WIN32 _codePage = GetConsoleOutputCP(); + + + CONSOLE_SCREEN_BUFFER_INFO info; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); + + _defaultAttribute = info.wAttributes & (FOREGROUND_INTENSITY | BACKGROUND_INTENSITY); + _defaultColor = info.wAttributes & FOREGROUND_WHITE; + _defaultBackground = info.wAttributes & BACKGROUND_WHITE; + + _consoleAttribute = _defaultAttribute; + _consoleColor = _defaultColor | _defaultBackground; #endif } @@ -132,10 +148,9 @@ void ConsoleFeature::stop() { } #ifdef _WIN32 -int CONSOLE_ATTRIBUTE = 0; -int CONSOLE_COLOR = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE; +static void _newLine() { fprintf(stdout, "\n"); } -static void _print2(std::string const& s) { +void ConsoleFeature::_print2(std::string const& s) { size_t sLen = s.size(); if (sLen == 0) { @@ -148,7 +163,7 @@ static void _print2(std::string const& s) { if (wLen) { auto handle = GetStdHandle(STD_OUTPUT_HANDLE); - SetConsoleTextAttribute(handle, CONSOLE_ATTRIBUTE | CONSOLE_COLOR); + SetConsoleTextAttribute(handle, _consoleAttribute | _consoleColor); DWORD n; WriteConsoleW(handle, wBuf, (DWORD)wLen, &n, NULL); @@ -162,9 +177,7 @@ static void _print2(std::string const& s) { } } -static void _newLine() { fprintf(stdout, "\n"); } - -static void _print(std::string const& s) { +void ConsoleFeature::_print(std::string const& s) { auto pos = s.find_first_of("\x1b"); if (pos == std::string::npos) { @@ -191,52 +204,45 @@ static void _print(std::string const& s) { } else if (c == 'm' || c == ';') { switch (code) { case 0: - CONSOLE_ATTRIBUTE = 0; - CONSOLE_COLOR = - FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE; + _consoleAttribute = _defaultAttribute; + _consoleColor = _defaultColor | _defaultBackground; break; case 1: // BOLD case 5: // BLINK - CONSOLE_ATTRIBUTE = FOREGROUND_INTENSITY; + _consoleAttribute = FOREGROUND_INTENSITY; break; case 30: - CONSOLE_COLOR = - BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN; + _consoleColor = BACKGROUND_WHITE; break; case 31: - CONSOLE_COLOR = FOREGROUND_RED; + _consoleColor = FOREGROUND_RED | _defaultBackground; break; case 32: - CONSOLE_COLOR = FOREGROUND_GREEN; + _consoleColor = FOREGROUND_GREEN | _defaultBackground; break; case 33: - CONSOLE_COLOR = FOREGROUND_RED | FOREGROUND_GREEN; + _consoleColor = FOREGROUND_RED | FOREGROUND_GREEN | _defaultBackground; break; case 34: - CONSOLE_COLOR = FOREGROUND_BLUE; + _consoleColor = FOREGROUND_BLUE | _defaultBackground; break; case 35: - CONSOLE_COLOR = FOREGROUND_BLUE | FOREGROUND_RED; + _consoleColor = FOREGROUND_BLUE | FOREGROUND_RED | _defaultBackground; break; case 36: - CONSOLE_COLOR = FOREGROUND_BLUE | FOREGROUND_GREEN; + _consoleColor = FOREGROUND_BLUE | FOREGROUND_GREEN | _defaultBackground; break; case 37: - CONSOLE_COLOR = - FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE; - break; - - case 39: - CONSOLE_COLOR = 0; + _consoleColor = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | _defaultBackground; break; } @@ -258,7 +264,7 @@ static void _print(std::string const& s) { } auto handle = GetStdHandle(STD_OUTPUT_HANDLE); - SetConsoleTextAttribute(handle, CONSOLE_ATTRIBUTE | CONSOLE_COLOR); + SetConsoleTextAttribute(handle, _consoleAttribute | _consoleColor); } #endif diff --git a/lib/ApplicationFeatures/ConsoleFeature.h b/lib/ApplicationFeatures/ConsoleFeature.h index 937738a0cf..9ff831b8ab 100644 --- a/lib/ApplicationFeatures/ConsoleFeature.h +++ b/lib/ApplicationFeatures/ConsoleFeature.h @@ -87,6 +87,19 @@ class ConsoleFeature final : public application_features::ApplicationFeature { void startPager(); void stopPager(); +#ifdef _WIN32 + private: + void _print2(std::string const&); + void _print(std::string const&); + + private: + int _consoleAttribute; + int _consoleColor; + int _defaultAttribute; + int _defaultColor; + int _defaultBackground; +#endif + private: bool _promptError; bool _supportsColors; diff --git a/lib/Basics/shell-colors.h b/lib/Basics/shell-colors.h index 31203c3d5c..109ee38c90 100644 --- a/lib/Basics/shell-colors.h +++ b/lib/Basics/shell-colors.h @@ -94,7 +94,7 @@ /// @brief color bold black //////////////////////////////////////////////////////////////////////////////// -#define TRI_SHELL_COLOR_BOLD_BLACK "\x1b[1;39m" +#define TRI_SHELL_COLOR_BOLD_BLACK "\x1b[1;30m" //////////////////////////////////////////////////////////////////////////////// /// @brief color cyan