Skip to content

Commit 2ac986f

Browse files
committed
Set console code page to CP_UTF8 on Windows if supported.
Previously, we enforced code page 850 for all console input and output, which breaks with non-western scripts. Since more recent Windows shells are able to display Unicode properly, this patch now enforces UTF-8 and falls back to code page 850 only if UTF-8 is unsupported. Resolves #3049.
1 parent c7898fd commit 2ac986f

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/cli/TextStream.cpp

+19-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
#include <QProcessEnvironment>
2121
#include <QTextCodec>
22+
#ifdef Q_OS_WIN
23+
#include <windows.h>
24+
#endif
2225

2326
TextStream::TextStream()
2427
{
@@ -57,14 +60,27 @@ TextStream::TextStream(const QByteArray& array, QIODevice::OpenMode openMode)
5760

5861
void TextStream::detectCodec()
5962
{
60-
QString codecName = "UTF-8";
63+
QString codecName = QTextCodec::codecForLocale()->name();
64+
if (codecName == "C") {
65+
// Force UTF-8 on *nix systems with a C locale or else Unicode
66+
// passwords will only be question marks.
67+
codecName = "UTF-8";
68+
}
6169
auto env = QProcessEnvironment::systemEnvironment();
70+
6271
#ifdef Q_OS_WIN
63-
if (!env.contains("SHELL")) {
64-
// native shell (no Msys or cygwin)
72+
codecName = "UTF-8";
73+
WINBOOL success = false;
74+
#ifdef CP_UTF8
75+
success = SetConsoleOutputCP(CP_UTF8);
76+
#endif
77+
if (!success && !env.contains("SHELL")) {
78+
// Fall back to cp850 if this is Windows without CP_UTF8 and we
79+
// are running in a native shell (i.e., no Msys or Cygwin).
6580
codecName = "Windows-850";
6681
}
6782
#endif
83+
6884
codecName = env.value("ENCODING_OVERRIDE", codecName);
6985
auto* codec = QTextCodec::codecForName(codecName.toLatin1());
7086
if (codec) {

0 commit comments

Comments
 (0)