forked from keepassxreboot/keepassxc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add interactive session mode to keepassxc-cli.
This change adds a GNU Readline-based interactive mode to keepassxc-cli. If GNU Readline is not available, commands are just read from stdin with no editing support. DatabaseCommand is modified to add the path to the current database to the arguments passed to executeWithDatabase. In this way, instances of DatabaseCommand do not have to prompt to re-open the database after each invocation, and existing command implementations do not have to be changed to support interactive mode. Fixes keepassxreboot#3224.
- Loading branch information
Showing
12 changed files
with
414 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Code copied from sethhall@github | ||
# | ||
# - Try to find readline include dirs and libraries | ||
# | ||
# Usage of this module as follows: | ||
# | ||
# find_package(Readline) | ||
# | ||
# Variables used by this module, they can change the default behaviour and need | ||
# to be set before calling find_package: | ||
# | ||
# Readline_ROOT_DIR Set this variable to the root installation of | ||
# readline if the module has problems finding the | ||
# proper installation path. | ||
# | ||
# Variables defined by this module: | ||
# | ||
# READLINE_FOUND System has readline, include and lib dirs found | ||
# Readline_INCLUDE_DIR The readline include directories. | ||
# Readline_LIBRARY The readline library. | ||
|
||
find_path(Readline_ROOT_DIR | ||
NAMES include/readline/readline.h | ||
) | ||
|
||
find_path(Readline_INCLUDE_DIR | ||
NAMES readline/readline.h | ||
HINTS ${Readline_ROOT_DIR}/include | ||
) | ||
|
||
find_library(Readline_LIBRARY | ||
NAMES readline | ||
HINTS ${Readline_ROOT_DIR}/lib | ||
) | ||
|
||
if(Readline_INCLUDE_DIR AND Readline_LIBRARY AND Ncurses_LIBRARY) | ||
set(READLINE_FOUND TRUE) | ||
else(Readline_INCLUDE_DIR AND Readline_LIBRARY AND Ncurses_LIBRARY) | ||
FIND_LIBRARY(Readline_LIBRARY NAMES readline) | ||
include(FindPackageHandleStandardArgs) | ||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Readline DEFAULT_MSG Readline_INCLUDE_DIR Readline_LIBRARY ) | ||
MARK_AS_ADVANCED(Readline_INCLUDE_DIR Readline_LIBRARY) | ||
endif(Readline_INCLUDE_DIR AND Readline_LIBRARY AND Ncurses_LIBRARY) | ||
|
||
mark_as_advanced( | ||
Readline_ROOT_DIR | ||
Readline_INCLUDE_DIR | ||
Readline_LIBRARY | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (C) 2019 KeePassXC Team <[email protected]> | ||
* | ||
* 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/>. | ||
*/ | ||
|
||
#include "CommandParser.h" | ||
|
||
QStringList const CommandParser::parse(const QString& command) | ||
{ | ||
QStringList result; | ||
|
||
bool inside_quotes = false; | ||
QString cur; | ||
for (int i = 0; i < command.size(); ++i) { | ||
QChar c = command[i]; | ||
if (c == '\\' && i < command.size() - 1) { | ||
cur.append(command[i + 1]); | ||
++i; | ||
} else if (!inside_quotes && (c == ' ' || c == '\t')) { | ||
if (!cur.isEmpty()) { | ||
result.append(cur); | ||
cur.clear(); | ||
} | ||
} else if (c == '"' && (inside_quotes || i == 0 || command[i - 1].isSpace())) { | ||
inside_quotes = !inside_quotes; | ||
} else { | ||
cur.append(c); | ||
} | ||
} | ||
|
||
if (!cur.isEmpty()) { | ||
result.append(cur); | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (C) 2019 KeePassXC Team <[email protected]> | ||
* | ||
* 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/>. | ||
*/ | ||
|
||
#ifndef KEEPASSXC_COMMANDPARSER_H | ||
#define KEEPASSXC_COMMANDPARSER_H | ||
|
||
#include <QtCore/QStringList> | ||
|
||
// Implements basic string splitting for the readline-based interactive CLI. | ||
class CommandParser | ||
{ | ||
public: | ||
CommandParser() = default; | ||
|
||
// Splits the given QString into a QString list. For example: | ||
// | ||
// "hello world" -> ["hello", "world"] | ||
// "hello world" -> ["hello", "world"] | ||
// "hello\\ world" -> ["hello world"] (i.e. backslash is an escape character | ||
// "\"hello world\"" -> ["hello world"] | ||
QStringList const parse(const QString& command); | ||
}; | ||
|
||
#endif // KEEPASSXC_COMMANDPARSER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (C) 2019 KeePassXC Team <[email protected]> | ||
* | ||
* 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/>. | ||
*/ | ||
|
||
#include "Open.h" | ||
|
||
#include <QtCore/QCommandLineParser> | ||
|
||
#include "DatabaseCommand.h" | ||
#include "TextStream.h" | ||
#include "Utils.h" | ||
|
||
Open::Open() | ||
{ | ||
name = QString("open"); | ||
description = QObject::tr("Open a database."); | ||
} | ||
|
||
int Open::executeWithDatabase(QSharedPointer<Database> db, QSharedPointer<QCommandLineParser> parser) | ||
{ | ||
Q_UNUSED(parser) | ||
currentDatabase = db; | ||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (C) 2019 KeePassXC Team <[email protected]> | ||
* | ||
* 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/>. | ||
*/ | ||
|
||
#ifndef KEEPASSXC_OPEN_H | ||
#define KEEPASSXC_OPEN_H | ||
|
||
#include "DatabaseCommand.h" | ||
|
||
class Open : public DatabaseCommand | ||
{ | ||
public: | ||
Open(); | ||
~Open() override = default; | ||
int executeWithDatabase(QSharedPointer<Database> db, QSharedPointer<QCommandLineParser> parser) override; | ||
}; | ||
|
||
#endif // KEEPASSXC_OPEN_H |
Oops, something went wrong.