diff --git a/src/cli/Utils.cpp b/src/cli/Utils.cpp index 703323eaaa..de12e6ba6c 100644 --- a/src/cli/Utils.cpp +++ b/src/cli/Utils.cpp @@ -43,6 +43,11 @@ namespace Utils QTextStream STDIN; QTextStream DEVNULL; +#ifdef Q_OS_WIN + UINT origCodePage; + UINT origOutputCodePage; +#endif + void setDefaultTextStreams() { auto fd = new QFile(); @@ -66,6 +71,9 @@ namespace Utils DEVNULL.setDevice(fd); #ifdef Q_OS_WIN + origCodePage = GetConsoleCP(); + origOutputCodePage = GetConsoleOutputCP(); + // On Windows, we ask via keepassxc-cli.exe.manifest to use UTF-8, // but the console code-page isn't automatically changed to match. SetConsoleCP(GetACP()); @@ -73,6 +81,14 @@ namespace Utils #endif } + void resetTextStreams() + { +#ifdef Q_OS_WIN + SetConsoleCP(origCodePage); + SetConsoleOutputCP(origOutputCodePage); +#endif + } + void setStdinEcho(bool enable = true) { #ifdef Q_OS_WIN diff --git a/src/cli/Utils.h b/src/cli/Utils.h index 84ddbbb4b3..6a272fc620 100644 --- a/src/cli/Utils.h +++ b/src/cli/Utils.h @@ -39,6 +39,7 @@ namespace Utils static const QStringList EntryFieldNames(QStringList() << UuidFieldName << TagsFieldName); void setDefaultTextStreams(); + void resetTextStreams(); void setStdinEcho(bool enable); bool loadFileKey(const QString& path, QSharedPointer& fileKey); diff --git a/src/cli/keepassxc-cli.cpp b/src/cli/keepassxc-cli.cpp index 67112e7ad9..2c4bd1aa29 100644 --- a/src/cli/keepassxc-cli.cpp +++ b/src/cli/keepassxc-cli.cpp @@ -181,6 +181,8 @@ int main(int argc, char** argv) QCoreApplication app(argc, argv); QCoreApplication::setApplicationVersion(KEEPASSXC_VERSION); + // Cleanup code pages after cli exits + QObject::connect(&app, &QCoreApplication::destroyed, &app, [] { Utils::resetTextStreams(); }); Bootstrap::bootstrap(config()->get(Config::GUI_Language).toString()); Utils::setDefaultTextStreams(); @@ -218,7 +220,9 @@ int main(int argc, char** argv) // Switch to parser.showVersion() when available (QT 5.4). out << KEEPASSXC_VERSION << Qt::endl; return EXIT_SUCCESS; - } else if (parser.isSet(debugInfoOption)) { + } + + if (parser.isSet(debugInfoOption)) { QString debugInfo = Tools::debugInfo().append("\n").append(Crypto::debugInfo()); out << debugInfo << Qt::endl; return EXIT_SUCCESS; diff --git a/src/main.cpp b/src/main.cpp index a3e736e204..a9243abe80 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -174,6 +174,8 @@ int main(int argc, char** argv) return EXIT_FAILURE; } + Utils::setDefaultTextStreams(); + // Apply the configured theme before creating any GUI elements app.applyTheme(); @@ -192,9 +194,6 @@ int main(int argc, char** argv) mainWindow.setAllowScreenCapture(parser.isSet(allowScreenCaptureOption)); const bool pwstdin = parser.isSet(pwstdinOption); - if (!fileNames.isEmpty() && pwstdin) { - Utils::setDefaultTextStreams(); - } for (const QString& filename : fileNames) { QString password; if (pwstdin) { @@ -228,5 +227,7 @@ int main(int argc, char** argv) __lsan_disable(); #endif + Utils::resetTextStreams(); + return exitCode; }