Skip to content

Commit

Permalink
CLI: Restore the original codepage on windows
Browse files Browse the repository at this point in the history
* Fixes #11465
  • Loading branch information
droidmonkey committed Dec 3, 2024
1 parent 9a63e80 commit b1180b3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/cli/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -66,13 +71,24 @@ 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());
SetConsoleOutputCP(GetACP());
#endif
}

void resetTextStreams()
{
#ifdef Q_OS_WIN
SetConsoleCP(origCodePage);
SetConsoleOutputCP(origOutputCodePage);
#endif
}

void setStdinEcho(bool enable = true)
{
#ifdef Q_OS_WIN
Expand Down
1 change: 1 addition & 0 deletions src/cli/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>& fileKey);
Expand Down
6 changes: 5 additions & 1 deletion src/cli/keepassxc-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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) {
Expand Down Expand Up @@ -228,5 +227,7 @@ int main(int argc, char** argv)
__lsan_disable();
#endif

Utils::resetTextStreams();

return exitCode;
}

0 comments on commit b1180b3

Please sign in to comment.