diff --git a/arangosh/ArangoShell/ArangoClient.cpp b/arangosh/ArangoShell/ArangoClient.cpp index 2ee0035c83..e90192fe53 100644 --- a/arangosh/ArangoShell/ArangoClient.cpp +++ b/arangosh/ArangoShell/ArangoClient.cpp @@ -493,35 +493,60 @@ void ArangoClient::stopPager () { #endif +//////////////////////////////////////////////////////////////////////////////// +/// @brief strip binary data from string +/// this is done before sending the string to a pager or writing it to the log +//////////////////////////////////////////////////////////////////////////////// + +static std::string StripBinary (const char* value) { + string result; + + char const* p = value; + bool inBinary = false; + + while (*p) { + if (inBinary) { + if (*p == 'm') { + inBinary = false; + } + } + else { + if (*p == '\x1b') { + inBinary = true; + } + else { + result.push_back(*p); + } + } + ++p; + } + + return result; +} + //////////////////////////////////////////////////////////////////////////////// /// @brief print to pager //////////////////////////////////////////////////////////////////////////////// void ArangoClient::internalPrint (const char* format, const char* str) { - if (str) { - if (*str == '\x1b') { - // terminal escape sequence - if (_pager == stdout) { - fprintf(_pager, format, str); - } - } - else { - // regular string value - fprintf(_pager, format, str); - log(format, str); + if (str == 0) { + str = format; + format = "%s"; + } + + if (_pager == stdout) { + fprintf(_pager, format, str); + if (_log) { + string sanitised = StripBinary(str); + log(format, sanitised.c_str()); } } else { - if (*format == '\x1b') { - // terminal escape sequence - if (_pager == stdout) { - fprintf(_pager, "%s", format); - } - } - else { - // regular string value - fprintf(_pager, "%s", format); - log("%s", format); + string sanitised = StripBinary(str); + + if (! sanitised.empty()) { + fprintf(_pager, format, sanitised.c_str()); + log(format, sanitised.c_str()); } } } @@ -534,7 +559,7 @@ void ArangoClient::openLog () { if (! _logFile.empty()) { _log = fopen(_logFile.c_str(), "w"); - ostringstream s; + ostringstream s; if (_log == 0) { s << "Cannot open file '" << _logFile << "' for logging."; printErrLine(s.str()); @@ -592,9 +617,11 @@ void ArangoClient::printByeBye () { void ArangoClient::log (const char* format, const char* str) { if (_log) { - if (*str != '\x1b') { + string sanitised = StripBinary(str); + + if (! sanitised.empty()) { // do not print terminal escape sequences into log - fprintf(_log, format, str); + fprintf(_log, format, sanitised.c_str()); } } } @@ -607,9 +634,11 @@ void ArangoClient::log (const char* format, const char* prompt, const char* str) { if (_log) { - if (*str != '\x1b') { + string sanitised = StripBinary(str); + + if (! sanitised.empty()) { // do not print terminal escape sequences into log - fprintf(_log, format, prompt, str); + fprintf(_log, format, prompt, sanitised.c_str()); } } } diff --git a/arangosh/V8Client/arangosh.cpp b/arangosh/V8Client/arangosh.cpp index 75519e790a..1223530ad6 100644 --- a/arangosh/V8Client/arangosh.cpp +++ b/arangosh/V8Client/arangosh.cpp @@ -1360,7 +1360,7 @@ static void RunShell (v8::Handle context, bool promptError) { // command failed string exception(TRI_StringifyV8Exception(&tryCatch)); - BaseClient.printErrLine(exception); + BaseClient.printErrLine(exception); BaseClient.log("%s", exception.c_str()); // this will change the prompt for the next round @@ -1368,7 +1368,7 @@ static void RunShell (v8::Handle context, bool promptError) { } BaseClient.stopPager(); - BaseClient.printLine(""); + BaseClient.printLine(""); BaseClient.log("%s\n", ""); // make sure the last command result makes it into the log file