8000 Bug fix/fix console codepage (#6646) · 0xflotus/arangodb@a1e3211 · GitHub
[go: up one dir, main page]

Skip to content

Commit a1e3211

Browse files
dothebartjsteemann
authored andcommitted
Bug fix/fix console codepage (arangodb#6646)
1 parent ea377b0 commit a1e3211

File tree

11 files changed

+97
-34
lines changed

11 files changed

+97
-34
lines changed

arangod/RestServer/InitDatabaseFeature.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222

2323
#include "InitDatabaseFeature.h"
2424

25+
#ifdef _WIN32
26+
#include <tchar.h>
27+
#include <unicode/locid.h>
28+
#include <string.h>
29+
#include <locale.h>
30+
#include <iomanip>
31+
#endif
32+
2533
#include <chrono>
2634
#include <iostream>
2735
#include <thread>
@@ -86,13 +94,7 @@ void InitDatabaseFeature::validateOptions(
8694

8795
void InitDatabaseFeature::prepare() {
8896
if (!_seenPassword) {
89-
std::string env = "ARANGODB_DEFAULT_ROOT_PASSWORD";
90-
char const* password = getenv(env.c_str());
91-
92-
if (password != nullptr) {
93-
env += "=";
94-
putenv(const_cast<char*>(env.c_str()));
95-
_password = password;
97+
if (TRI_GETENV("ARANGODB_DEFAULT_ROOT_PASSWORD", _password)){
9698
_seenPassword = true;
9799
}
98100
}
@@ -134,7 +136,15 @@ std::string InitDatabaseFeature::readPassword(std::string const& message) {
134136
std::this_thread::sleep_for(std::chrono::milliseconds(500));
135137
std::cerr << std::flush;
136138
std::cout << message << ": " << std::flush;
137-
139+
#ifdef _WIN32
140+
TRI_SetStdinVisibility(false);
141+
TRI_DEFER(TRI_SetStdinVisibility(true));
142+
std::wstring wpassword;
143+
_setmode(_fileno(stdin), _O_U16TEXT);
144+
std::getline(std::wcin, wpassword);
145+
UnicodeString pw(wpassword.c_str(), wpassword.length());
146+
pw.toUTF8String<std::string>(password);
147+
#else
138148
#ifdef TRI_HAVE_TERMIOS_H
139149
TRI_SetStdinVisibility(false);
140150
std::getline(std::cin, password);
@@ -143,7 +153,7 @@ std::string InitDatabaseFeature::readPassword(std::string const& message) {
143153
#else
144154
std::getline(std::cin, password);
145155
#endif
146-
156+
#endif
147157
std::cout << std::endl;
148158

149159
return password;

arangod/RestServer/ServerFeature.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ ServerFeature::ServerFeature(
5252
: ApplicationFeature(server, "Server"),
5353
_vstMaxSize(1024 * 30),
5454
_result(res),
55-
_operationMode(OperationMode::MODE_SERVER) {
55+
_operationMode(OperationMode::MODE_SERVER),
56+
_codePage(65001) { // default to UTF8
5657
setOptional(true);
5758

5859
startsAfter("AQLPhase");
@@ -84,6 +85,10 @@ void ServerFeature::collectOptions(std::shared_ptr<ProgramOptions> options) {
8485
options->addOption("--vst.maxsize",
8586
"maximal size (in bytes) for a VelocyPack chunk",
8687
new UInt32Parameter(&_vstMaxSize));
88+
#if _WIN32
89+
options->addHiddenOption("--console.code-page", "Windows code page to use; defaults to UTF8",
90+
new UInt16Parameter(&_codePage));
91+
#endif
8792
}
8893

8994
void ServerFeature::validateOptions(std::shared_ptr<ProgramOptions> options) {
@@ -152,6 +157,10 @@ void ServerFeature::validateOptions(std::shared_ptr<ProgramOptions> options) {
152157
}
153158

154159
void ServerFeature::start() {
160+
#if _WIN32
161+
SetConsoleOutputCP(_codePage);
162+
#endif
163+
155164
waitForHeartbeat();
156165

157166
*_result = EXIT_SUCCESS;

arangod/RestServer/ServerFeature.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ class ServerFeature final : public application_features::ApplicationFeature {
7070
int* _result;
7171
OperationMode _operationMode;
7272
bool _isStopping = false;
73+
uint16_t _codePage;
7374
};
7475

7576
}
7677

77-
#endif
78+
#endif

arangosh/Shell/ClientFeature.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ ClientFeature::ClientFeature(
6464
_retries(DEFAULT_RETRIES),
6565
_warn(false),
6666
_warnConnect(true),
67-
_haveServerPassword(false) {
67+
_haveServerPassword(false),
68+
_codePage(65001), // default to UTF8
69+
_originalCodePage(-1) {
6870
setOptional(true);
6971
requiresElevatedPrivileges(false);
7072
startsAfter("GreetingsPhase");
@@ -130,6 +132,10 @@ void ClientFeature::collectOptions(std::shared_ptr<ProgramOptions> options) {
130132
availableSslProtocolsDescription(),
131133
new DiscreteValuesParameter<UInt64Parameter>(
132134
&_sslProtocol, sslProtocols));
135+
#if _WIN32
136+
options->addHiddenOption("--console.code-page", "Windows code page to use; defaults to UTF8",
137+
new UInt16Parameter(&_codePage));
138+
#endif
133139
}
134140

135141
void ClientFeature::validateOptions(std::shared_ptr<ProgramOptions> options) {
@@ -302,6 +308,20 @@ std::vector<std::string> ClientFeature::httpEndpoints() {
302308
return {http};
303309
}
304310

311+
void ClientFeature::start() {
312+
#if _WIN32
313+
_originalCodePage = GetConsoleOutputCP();
314+
315+
SetConsoleOutputCP(_codePage);
316+
#endif
317+
}
318+
319+
void ClientFeature::stop() {
320+
#if _WIN32
321+
SetConsoleOutputCP(_originalCodePage);
322+
#endif
323+
}
324+
305325
int ClientFeature::runMain(
306326
int argc, char* argv[],
307327
std::function<int(int argc, char* argv[])> const& mainFunc) {
@@ -319,5 +339,5 @@ int ClientFeature::runMain(
319339
return EXIT_FAILURE;
320340
}
321341
}
322-
342+
323343
} // arangodb

arangosh/Shell/ClientFeature.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class ClientFeature final : public application_features::ApplicationFeature,
5656
void collectOptions(std::shared_ptr<options::ProgramOptions>) override final;
5757
void validateOptions(std::shared_ptr<options::ProgramOptions>) override final;
5858
void prepare() override final;
59+
void start() override final;
60+
void stop() override final;
5961

6062
std::string const& databaseName() const { return _databaseName; }
6163
bool authentication() const { return _authentication; }
@@ -121,8 +123,10 @@ class ClientFeature final : public application_features::ApplicationFeature,
121123
bool _warn;
122124
bool _warnConnect;
123125
bool _haveServerPassword;
126+
uint16_t _codePage;
127+
uint16_t _originalCodePage;
124128
};
125129

126130
} // namespace arangodb
127131

128-
#endif
132+
#endif

arangosh/Shell/ConsoleFeature.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
/// @author Dr. Frank Celler
2121
////////////////////////////////////////////////////////////////////////////////
2222

23+
#ifdef _WIN32
24+
#include <tchar.h>
25+
#include <unicode/locid.h>
26+
#include <string.h>
27+
#include <locale.h>
28+
#endif
29+
2330
#include "ConsoleFeature.h"
2431

2532
#include "ApplicationFeatures/ShellColorsFeature.h"
@@ -50,7 +57,6 @@ namespace arangodb {
5057
ConsoleFeature::ConsoleFeature(application_features::ApplicationServer& server)
5158
: ApplicationFeature(server, "Console"),
5259
#ifdef _WIN32
53-
_codePage(-1),
5460
_cygwinShell(false),
5561
#endif
5662
_quiet(false),
@@ -70,14 +76,11 @@ ConsoleFeature::ConsoleFeature(application_features::ApplicationServer& server)
7076
setOptional(false);
7177
requiresElevatedPrivileges(false);
7278
startsAfter("BasicsPhase");
73-
7479
if (!_supportsColors) {
7580
_colors = false;
7681
}
7782

7883
#if _WIN32
79-
_codePage = GetConsoleOutputCP();
80-
8184
CONSOLE_SCREEN_BUFFER_INFO info;
8285
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
8386

@@ -117,11 +120,6 @@ void ConsoleFeature::collectOptions(std::shared_ptr<ProgramOptions> options) {
117120

118121
options->addOption("--console.prompt", "prompt used in REPL. prompt components are: '%t': current time as timestamp, '%p': duration of last command in seconds, '%d': name of current database, '%e': current endpoint, '%E': current endpoint without protocol, '%u': current user",
119122
new StringParameter(&_prompt));
120-
121-
#if _WIN32
122-
options->addHiddenOption(&qu F438 ot;--console.code-page", "Windows code page to use",
123-
new UInt16Parameter(&_codePage));
124-
#endif
125123
}
126124

127125
void ConsoleFeature::prepare() {
@@ -134,12 +132,6 @@ void ConsoleFeature::prepare() {
134132

135133
void ConsoleFeature::start() {
136134
openLog();
137-
138-
#if _WIN32
139-
if (_codePage != -1) {
140-
SetConsoleOutputCP(_codePage);
141-
}
142-
#endif
143135
}
144136

145137
void ConsoleFeature::unprepare() {
@@ -309,7 +301,16 @@ std::string ConsoleFeature::readPassword() {
309301
TRI_DEFER(TRI_SetStdinVisibility(true));
310302

311303
std::string password;
304+
305+
#ifdef _WIN32
306+
std::wstring wpassword;
307+
_setmode(_fileno(stdin), _O_U16TEXT);
308+
std::getline(std::wcin, wpassword);
309+
UnicodeString pw(wpassword.c_str(), static_cast<int32_t>(wpassword.length()));
310+
pw.toUTF8String<std::string>(password);
311+
#else
312312
std::getline(std::cin, password);
313+
#endif
313314
return password;
314315
}
315316

arangosh/Shell/ConsoleFeature.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class ConsoleFeature final : public application_features::ApplicationFeature {
5050

5151
private:
5252
#ifdef _WIN32
53-
uint16_t _codePage;
5453
bool _cygwinShell;
5554
#endif
5655
bool _quiet;
@@ -114,4 +113,4 @@ class ConsoleFeature final : public application_features::ApplicationFeature {
114113

115114
}
116115

117-
#endif
116+
#endif

lib/ApplicationFeatures/LanguageFeature.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ void* LanguageFeature::prepareIcu(std::string const& binaryPath,
9999
std::string msg = std::string("cannot locate '") + path +
100100
"'; please make sure it is available; "
101101
"the variable ICU_DATA='";
102-
if (getenv("ICU_DATA") != nullptr) {
103-
msg += getenv("ICU_DATA");
102+
std::string icupath;
103+
if ( TRI_GETENV("ICU_DATA", icupath)) {
104+
msg += icupath;
104105
}
105106
msg += "' should point to the directory containing '" + fn + "'";
106107

lib/Basics/FileUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ std::vector<std::string> listFiles(std::string const& directory) {
404404
continue;
405405
}
406406

407+
result.push_back(rcs);
407408
} while (_wfindnext(handle, &oneItem) != -1);
408409

409410
_findclose(handle);

lib/V8/v8-globals.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ static inline v8::Handle<v8::String> v8Utf8StringFactory(v8::Isolate* isolate, v
279279
isolate, WHAT.c_str(), v8::String::kNormalString, (int)WHAT.length())); \
280280
return
281281

282+
/// @brief return a std::wstring
283+
/// implicitly requires 'args and 'isolate' to be available
284+
/// @param WHAT the name of the std::string variable
285+
#define TRI_V8_RETURN_STD_WSTRING(WHAT) \
286+
args.GetReturnValue().Set(v8::String::NewFromTwoByte( \
287+
isolate, (const uint16_t *)WHAT.c_str(), v8::String::kNormalString, (int)WHAT.length())); \
288+
return
289+
282290
/// @brief return a string which you know the length of
283291
/// implicitly requires 'args and 'isolate' to be available
284292
/// @param WHAT the name of the char* variable
@@ -732,4 +740,4 @@ void TRI_AddGlobalVariableVocbase(v8::Isolate* isolate,
732740
v8::Handle<v8::String> name,
733741
v8::Handle<v8::Value> value);
734742

735-
#endif
743+
#endif

0 commit comments

Comments
 (0)
0