2017-01-31 00:18:35 +00:00
|
|
|
/*
|
2017-06-09 21:40:36 +00:00
|
|
|
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
2017-01-31 00:18:35 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
|
* version 3 of the License.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-02-02 22:30:54 +00:00
|
|
|
#include <cstdlib>
|
2017-01-31 00:18:35 +00:00
|
|
|
|
|
|
|
|
#include <QCommandLineParser>
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QStringList>
|
2017-06-19 15:09:19 +00:00
|
|
|
#include <QTextStream>
|
2017-01-31 00:18:35 +00:00
|
|
|
|
2017-07-17 19:16:53 +00:00
|
|
|
#include <cli/Command.h>
|
2017-03-12 17:34:56 +00:00
|
|
|
|
2017-01-31 00:18:35 +00:00
|
|
|
#include "config-keepassx.h"
|
2017-02-02 15:33:50 +00:00
|
|
|
#include "core/Tools.h"
|
2017-01-31 00:18:35 +00:00
|
|
|
#include "crypto/Crypto.h"
|
|
|
|
|
|
2017-03-16 17:41:12 +00:00
|
|
|
#if defined(WITH_ASAN) && defined(WITH_LSAN)
|
|
|
|
|
#include <sanitizer/lsan_interface.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-05-19 18:04:11 +00:00
|
|
|
int main(int argc, char** argv)
|
2017-01-31 00:18:35 +00:00
|
|
|
{
|
2017-02-02 15:33:50 +00:00
|
|
|
#ifdef QT_NO_DEBUG
|
|
|
|
|
Tools::disableCoreDumps();
|
|
|
|
|
#endif
|
2017-01-31 00:18:35 +00:00
|
|
|
|
|
|
|
|
if (!Crypto::init()) {
|
|
|
|
|
qFatal("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString()));
|
2017-02-02 22:30:54 +00:00
|
|
|
return EXIT_FAILURE;
|
2017-01-31 00:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
2017-07-17 19:16:53 +00:00
|
|
|
QTextStream out(stdout);
|
2017-06-19 15:09:19 +00:00
|
|
|
QStringList arguments;
|
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
|
arguments << QString(argv[i]);
|
|
|
|
|
}
|
2017-01-31 00:18:35 +00:00
|
|
|
QCommandLineParser parser;
|
2017-02-16 02:01:50 +00:00
|
|
|
|
|
|
|
|
QString description("KeePassXC command line interface.");
|
2017-07-17 19:16:53 +00:00
|
|
|
description = description.append(QObject::tr("\n\nAvailable commands:\n"));
|
|
|
|
|
for (Command* command : Command::getCommands()) {
|
|
|
|
|
description = description.append(command->getDescriptionLine());
|
|
|
|
|
}
|
|
|
|
|
parser.setApplicationDescription(description);
|
2017-02-16 02:01:50 +00:00
|
|
|
|
2017-07-17 19:16:53 +00:00
|
|
|
parser.addPositionalArgument("command", QObject::tr("Name of the command to execute."));
|
2017-01-31 00:18:35 +00:00
|
|
|
|
|
|
|
|
parser.addHelpOption();
|
|
|
|
|
parser.addVersionOption();
|
2017-06-19 15:09:19 +00:00
|
|
|
// TODO : use the setOptionsAfterPositionalArgumentsMode (Qt 5.6) function
|
|
|
|
|
// when available. Until then, options passed to sub-commands won't be
|
2017-02-02 22:54:39 +00:00
|
|
|
// recognized by this parser.
|
2017-06-19 15:09:19 +00:00
|
|
|
parser.parse(arguments);
|
2017-01-31 00:18:35 +00:00
|
|
|
|
2017-06-19 15:09:19 +00:00
|
|
|
if (parser.positionalArguments().size() < 1) {
|
2017-05-22 21:53:41 +00:00
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
|
app.setApplicationVersion(KEEPASSX_VERSION);
|
2017-06-19 15:09:19 +00:00
|
|
|
if (parser.isSet("version")) {
|
|
|
|
|
// Switch to parser.showVersion() when available (QT 5.4).
|
2017-07-17 19:16:53 +00:00
|
|
|
out << KEEPASSX_VERSION << endl;
|
2017-06-19 15:09:19 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
2017-01-31 00:18:35 +00:00
|
|
|
parser.showHelp();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 15:09:19 +00:00
|
|
|
QString commandName = parser.positionalArguments().at(0);
|
2017-07-17 19:16:53 +00:00
|
|
|
Command* command = Command::getCommand(commandName);
|
2017-01-31 00:18:35 +00:00
|
|
|
|
2017-07-17 19:16:53 +00:00
|
|
|
if (command == nullptr) {
|
2017-03-16 17:41:12 +00:00
|
|
|
qCritical("Invalid command %s.", qPrintable(commandName));
|
2017-05-22 21:53:41 +00:00
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
|
app.setApplicationVersion(KEEPASSX_VERSION);
|
2017-06-19 15:09:19 +00:00
|
|
|
// showHelp exits the application immediately, so we need to set the
|
|
|
|
|
// exit code here.
|
|
|
|
|
parser.showHelp(EXIT_FAILURE);
|
2017-02-02 22:54:39 +00:00
|
|
|
}
|
|
|
|
|
|
2017-07-17 19:16:53 +00:00
|
|
|
int exitCode = command->execute(argc, argv);
|
|
|
|
|
|
2017-03-16 17:41:12 +00:00
|
|
|
#if defined(WITH_ASAN) && defined(WITH_LSAN)
|
|
|
|
|
// do leak check here to prevent massive tail of end-of-process leak errors from third-party libraries
|
|
|
|
|
__lsan_do_leak_check();
|
|
|
|
|
__lsan_disable();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return exitCode;
|
2017-01-31 00:18:35 +00:00
|
|
|
}
|