Skip to content

Commit

Permalink
Write CLI diagnostic messages to stderr.
Browse files Browse the repository at this point in the history
As mentioned in 1dce5b0, POSIX specifies that all diagnostic and error
messages are to be sent to stderr, only meaningful data shall be output
to stdout (for example: attributes retrieved via keepassxc-cli show).

Stream variables were renamed using the <stream>TextStream convention
for consistency's sake.

It should be noted that stderr is supposed to be unbuffered, some calls
to flush() might not be useful anymore.
  • Loading branch information
lhark committed Jul 10, 2018
1 parent 1dce5b0 commit becad33
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 47 deletions.
10 changes: 5 additions & 5 deletions src/cli/Add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int Add::execute(const QStringList& arguments)
{

QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
QTextStream errorTextStream(stderr, QIODevice::WriteOnly);

QCommandLineParser parser;
parser.setApplicationDescription(this->description);
Expand Down Expand Up @@ -85,7 +85,7 @@ int Add::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 2) {
outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli add");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli add");
return EXIT_FAILURE;
}

Expand Down Expand Up @@ -120,8 +120,8 @@ int Add::execute(const QStringList& arguments)
}

if (parser.isSet(prompt)) {
outputTextStream << "Enter password for new entry: ";
outputTextStream.flush();
errorTextStream << "Enter password for new entry: ";
errorTextStream.flush();
QString password = Utils::getPassword();
entry->setPassword(password);
} else if (parser.isSet(generate)) {
Expand All @@ -145,6 +145,6 @@ int Add::execute(const QStringList& arguments)
return EXIT_FAILURE;
}

outputTextStream << "Successfully added entry " << entry->title() << "." << endl;
errorTextStream << "Successfully added entry " << entry->title() << "." << endl;
return EXIT_SUCCESS;
}
14 changes: 7 additions & 7 deletions src/cli/Clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Clip::~Clip()
int Clip::execute(const QStringList& arguments)
{

QTextStream out(stdout);
QTextStream errorTextStream(stderr);

QCommandLineParser parser;
parser.setApplicationDescription(this->description);
Expand All @@ -60,7 +60,7 @@ int Clip::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 2 && args.size() != 3) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli clip");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli clip");
return EXIT_FAILURE;
}

Expand All @@ -83,7 +83,7 @@ int Clip::clipEntry(Database* database, QString entryPath, QString timeout)
timeoutSeconds = timeout.toInt();
}

QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
QTextStream errorTextStream(stderr, QIODevice::WriteOnly);
Entry* entry = database->rootGroup()->findEntry(entryPath);
if (!entry) {
qCritical("Entry %s not found.", qPrintable(entryPath));
Expand All @@ -95,20 +95,20 @@ int Clip::clipEntry(Database* database, QString entryPath, QString timeout)
return exitCode;
}

outputTextStream << "Entry's password copied to the clipboard!" << endl;
errorTextStream << "Entry's password copied to the clipboard!" << endl;

if (!timeoutSeconds) {
return exitCode;
}

while (timeoutSeconds > 0) {
outputTextStream << "\rClearing the clipboard in " << timeoutSeconds << " seconds...";
outputTextStream.flush();
errorTextStream << "\rClearing the clipboard in " << timeoutSeconds << " seconds...";
errorTextStream.flush();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
timeoutSeconds--;
}
Utils::clipText("");
outputTextStream << "\nClipboard cleared!" << endl;
errorTextStream << "\nClipboard cleared!" << endl;

return EXIT_SUCCESS;
}
5 changes: 3 additions & 2 deletions src/cli/Diceware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ int Diceware::execute(const QStringList& arguments)
{
QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
QTextStream errorTextStream(stderr, QIODevice::WriteOnly);

QCommandLineParser parser;
parser.setApplicationDescription(this->description);
Expand All @@ -56,7 +57,7 @@ int Diceware::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (!args.isEmpty()) {
outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli diceware");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli diceware");
return EXIT_FAILURE;
}

Expand All @@ -76,7 +77,7 @@ int Diceware::execute(const QStringList& arguments)
}

if (!dicewareGenerator.isValid()) {
outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli diceware");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli diceware");
return EXIT_FAILURE;
}

Expand Down
10 changes: 5 additions & 5 deletions src/cli/Edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int Edit::execute(const QStringList& arguments)
{

QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
QTextStream errorTextStream(stderr, QIODevice::WriteOnly);

QCommandLineParser parser;
parser.setApplicationDescription(this->description);
Expand Down Expand Up @@ -91,7 +91,7 @@ int Edit::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 2) {
outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli edit");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli edit");
return EXIT_FAILURE;
}

Expand Down Expand Up @@ -137,8 +137,8 @@ int Edit::execute(const QStringList& arguments)
}

if (parser.isSet(prompt)) {
outputTextStream << "Enter new password for entry: ";
outputTextStream.flush();
errorTextStream << "Enter new password for entry: ";
errorTextStream.flush();
QString password = Utils::getPassword();
entry->setPassword(password);
} else if (parser.isSet(generate)) {
Expand All @@ -164,6 +164,6 @@ int Edit::execute(const QStringList& arguments)
return EXIT_FAILURE;
}

outputTextStream << "Successfully edited entry " << entry->title() << "." << endl;
errorTextStream << "Successfully edited entry " << entry->title() << "." << endl;
return EXIT_SUCCESS;
}
4 changes: 2 additions & 2 deletions src/cli/Estimate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static void estimate(const char* pwd, bool advanced)
int Estimate::execute(const QStringList& arguments)
{
QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
QTextStream errorTextStream(stderr, QIODevice::WriteOnly);

QCommandLineParser parser;
parser.setApplicationDescription(this->description);
Expand All @@ -154,7 +154,7 @@ int Estimate::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() > 1) {
outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli estimate");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli estimate");
return EXIT_FAILURE;
}

Expand Down
10 changes: 5 additions & 5 deletions src/cli/Extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ Extract::~Extract()

int Extract::execute(const QStringList& arguments)
{
QTextStream out(stdout);
QTextStream errorTextStream(stderr);
QTextStream outputTextStream(stdout);

QCommandLineParser parser;
parser.setApplicationDescription(this->description);
Expand All @@ -58,12 +58,12 @@ int Extract::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 1) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli extract");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli extract");
return EXIT_FAILURE;
}

out << QObject::tr("Insert password to unlock %1: ").arg(args.at(0));
out.flush();
errorTextStream << QObject::tr("Insert password to unlock %1: ").arg(args.at(0));
errorTextStream.flush();

CompositeKey compositeKey;

Expand Down Expand Up @@ -119,7 +119,7 @@ int Extract::execute(const QStringList& arguments)
return EXIT_FAILURE;
}

out << xmlData.constData() << "\n";
outputTextStream << xmlData.constData() << "\n";

return EXIT_SUCCESS;
}
5 changes: 3 additions & 2 deletions src/cli/Generate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ int Generate::execute(const QStringList& arguments)
{
QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
QTextStream errorTextStream(stderr, QIODevice::WriteOnly);

QCommandLineParser parser;
parser.setApplicationDescription(this->description);
Expand Down Expand Up @@ -83,7 +84,7 @@ int Generate::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (!args.isEmpty()) {
outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli generate");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli generate");
return EXIT_FAILURE;
}

Expand Down Expand Up @@ -128,7 +129,7 @@ int Generate::execute(const QStringList& arguments)
passwordGenerator.setExcludedChars(parser.value(exclude));

if (!passwordGenerator.isValid()) {
outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli generate");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli generate");
return EXIT_FAILURE;
}

Expand Down
4 changes: 2 additions & 2 deletions src/cli/List.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ List::~List()

int List::execute(const QStringList& arguments)
{
QTextStream out(stdout);
QTextStream errorTextStream(stderr);

QCommandLineParser parser;
parser.setApplicationDescription(this->description);
Expand All @@ -54,7 +54,7 @@ int List::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 1 && args.size() != 2) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli ls");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli ls");
return EXIT_FAILURE;
}

Expand Down
10 changes: 5 additions & 5 deletions src/cli/Locate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Locate::~Locate()
int Locate::execute(const QStringList& arguments)
{

QTextStream out(stdout);
QTextStream errorTextStream(stderr);

QCommandLineParser parser;
parser.setApplicationDescription(this->description);
Expand All @@ -57,7 +57,7 @@ int Locate::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 2) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli locate");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli locate");
return EXIT_FAILURE;
}

Expand All @@ -72,15 +72,15 @@ int Locate::execute(const QStringList& arguments)
int Locate::locateEntry(Database* database, QString searchTerm)
{

QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
QTextStream errorTextStream(stderr, QIODevice::WriteOnly);
QStringList results = database->rootGroup()->locate(searchTerm);
if (results.isEmpty()) {
outputTextStream << "No results for that search term" << endl;
errorTextStream << "No results for that search term" << endl;
return EXIT_SUCCESS;
}

for (QString result : results) {
outputTextStream << result << endl;
errorTextStream << result << endl;
}
return EXIT_SUCCESS;
}
6 changes: 3 additions & 3 deletions src/cli/Merge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Merge::~Merge()

int Merge::execute(const QStringList& arguments)
{
QTextStream out(stdout);
QTextStream errorTextStream(stderr);

QCommandLineParser parser;
parser.setApplicationDescription(this->description);
Expand All @@ -63,7 +63,7 @@ int Merge::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 2) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli merge");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli merge");
return EXIT_FAILURE;
}

Expand All @@ -90,6 +90,6 @@ int Merge::execute(const QStringList& arguments)
return EXIT_FAILURE;
}

out << "Successfully merged the database files.\n";
errorTextStream << "Successfully merged the database files.\n";
return EXIT_SUCCESS;
}
10 changes: 5 additions & 5 deletions src/cli/Remove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Remove::~Remove()

int Remove::execute(const QStringList& arguments)
{
QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
QTextStream errorTextStream(stderr, QIODevice::WriteOnly);

QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main", "Remove an entry from the database."));
Expand All @@ -59,7 +59,7 @@ int Remove::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 2) {
outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli rm");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli rm");
return EXIT_FAILURE;
}

Expand All @@ -74,7 +74,7 @@ int Remove::execute(const QStringList& arguments)
int Remove::removeEntry(Database* database, QString databasePath, QString entryPath)
{

QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
QTextStream errorTextStream(stderr, QIODevice::WriteOnly);
Entry* entry = database->rootGroup()->findEntryByPath(entryPath);
if (!entry) {
qCritical("Entry %s not found.", qPrintable(entryPath));
Expand All @@ -97,9 +97,9 @@ int Remove::removeEntry(Database* database, QString databasePath, QString entryP
}

if (recycled) {
outputTextStream << "Successfully recycled entry " << entryTitle << "." << endl;
errorTextStream << "Successfully recycled entry " << entryTitle << "." << endl;
} else {
outputTextStream << "Successfully deleted entry " << entryTitle << "." << endl;
errorTextStream << "Successfully deleted entry " << entryTitle << "." << endl;
}

return EXIT_SUCCESS;
Expand Down
4 changes: 2 additions & 2 deletions src/cli/Show.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Show::~Show()

int Show::execute(const QStringList& arguments)
{
QTextStream out(stdout);
QTextStream errorTextStream(stderr);

QCommandLineParser parser;
parser.setApplicationDescription(this->description);
Expand All @@ -63,7 +63,7 @@ int Show::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 2) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli show");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli show");
return EXIT_FAILURE;
}

Expand Down
4 changes: 2 additions & 2 deletions src/cli/keepassxc-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main(int argc, char** argv)
QCoreApplication app(argc, argv);
app.setApplicationVersion(KEEPASSX_VERSION);

QTextStream out(stdout);
QTextStream outputTextStream(stdout);
QStringList arguments;
for (int i = 0; i < argc; ++i) {
arguments << QString(argv[i]);
Expand All @@ -72,7 +72,7 @@ int main(int argc, char** argv)
if (parser.positionalArguments().size() < 1) {
if (parser.isSet("version")) {
// Switch to parser.showVersion() when available (QT 5.4).
out << KEEPASSX_VERSION << endl;
outputTextStream << KEEPASSX_VERSION << endl;
return EXIT_SUCCESS;
}
parser.showHelp();
Expand Down

0 comments on commit becad33

Please sign in to comment.