2017-04-26 22:56:19 +00:00
|
|
|
#include <QScrollBar>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QCompleter>
|
2017-04-27 23:57:13 +00:00
|
|
|
#include <QAction>
|
|
|
|
#include <QShortcut>
|
2017-04-26 22:56:19 +00:00
|
|
|
#include <QStringListModel>
|
2017-09-25 12:55:41 +00:00
|
|
|
#include "cutter.h"
|
2017-10-02 16:18:40 +00:00
|
|
|
#include "ConsoleWidget.h"
|
|
|
|
#include "ui_ConsoleWidget.h"
|
2017-10-01 19:09:42 +00:00
|
|
|
#include "utils/Helpers.h"
|
2017-04-26 22:56:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
// TODO: Find a way to get to this without copying it here
|
|
|
|
// source: libr/core/core.c:585..
|
2017-04-27 23:57:13 +00:00
|
|
|
// remark: u.* is missing
|
|
|
|
static const QStringList radareArgs(
|
|
|
|
{
|
2017-04-26 22:56:19 +00:00
|
|
|
"?", "?v", "whereis", "which", "ls", "rm", "mkdir", "pwd", "cat", "less",
|
|
|
|
"dH", "ds", "dso", "dsl", "dc", "dd", "dm", "db ", "db-",
|
2017-04-27 23:57:13 +00:00
|
|
|
"dp", "dr", "dcu", "dmd", "dmp", "dml",
|
|
|
|
"ec", "ecs", "eco",
|
2017-04-26 22:56:19 +00:00
|
|
|
"S", "S.", "S*", "S-", "S=", "Sa", "Sa-", "Sd", "Sl", "SSj", "Sr",
|
|
|
|
"s", "s+", "s++", "s-", "s--", "s*", "sa", "sb", "sr",
|
|
|
|
"!", "!!",
|
|
|
|
"#sha1", "#crc32", "#pcprint", "#sha256", "#sha512", "#md4", "#md5",
|
|
|
|
"#!python", "#!perl", "#!vala",
|
|
|
|
"V", "v",
|
|
|
|
"aa", "ab", "af", "ar", "ag", "at", "a?", "ax", "ad",
|
|
|
|
"ae", "aec", "aex", "aep", "aea", "aeA", "aes", "aeso", "aesu", "aesue", "aer", "aei", "aeim", "aef",
|
2017-04-27 23:57:13 +00:00
|
|
|
"aaa", "aac", "aae", "aai", "aar", "aan", "aas", "aat", "aap", "aav",
|
2017-04-26 22:56:19 +00:00
|
|
|
"af", "afa", "afan", "afc", "afC", "afi", "afb", "afbb", "afn", "afr", "afs", "af*", "afv", "afvn",
|
|
|
|
"aga", "agc", "agd", "agl", "agfl",
|
2017-04-27 23:57:13 +00:00
|
|
|
// see forbbidenArgs
|
|
|
|
//"e", "et", "e-", "e*", "e!", "e?", "env ",
|
2017-04-26 22:56:19 +00:00
|
|
|
"i", "ii", "iI", "is", "iS", "iz",
|
|
|
|
"q", "q!",
|
|
|
|
"f", "fl", "fr", "f-", "f*", "fs", "fS", "fr", "fo", "f?",
|
|
|
|
"m", "m*", "ml", "m-", "my", "mg", "md", "mp", "m?",
|
|
|
|
"o", "o+", "oc", "on", "op", "o-", "x", "wf", "wF", "wta", "wtf", "wp",
|
|
|
|
"t", "to", "t-", "tf", "td", "td-", "tb", "tn", "te", "tl", "tk", "ts", "tu",
|
|
|
|
"(", "(*", "(-", "()", ".", ".!", ".(", "./",
|
|
|
|
"r", "r+", "r-",
|
|
|
|
"b", "bf", "b?",
|
|
|
|
"/", "//", "/a", "/c", "/h", "/m", "/x", "/v", "/v2", "/v4", "/v8", "/r", "/re",
|
|
|
|
"y", "yy", "y?",
|
|
|
|
"wx", "ww", "w?", "wxf",
|
|
|
|
"p6d", "p6e", "p8", "pb", "pc",
|
|
|
|
"pd", "pda", "pdb", "pdc", "pdj", "pdr", "pdf", "pdi", "pdl", "pds", "pdt",
|
|
|
|
"pD", "px", "pX", "po", "pf", "pf.", "pf*", "pf*.", "pfd", "pfd.", "pv", "p=", "p-",
|
|
|
|
"pfj", "pfj.", "pfv", "pfv.",
|
|
|
|
"pm", "pr", "pt", "ptd", "ptn", "pt?", "ps", "pz", "pu", "pU", "p?",
|
|
|
|
"z", "z*", "zj", "z-", "z-*",
|
|
|
|
"za", "zaf", "zaF",
|
|
|
|
"zo", "zoz", "zos",
|
|
|
|
"zfd", "zfs", "zfz",
|
|
|
|
"z/", "z/*",
|
|
|
|
"zc",
|
|
|
|
"zs", "zs+", "zs-", "zs-*", "zsr",
|
2017-04-27 23:57:13 +00:00
|
|
|
"#!pipe"
|
|
|
|
});
|
|
|
|
|
|
|
|
static const QStringList forbiddenArgs({"e", "et", "e-", "e*", "e!", "e?", "env"});
|
|
|
|
|
|
|
|
|
|
|
|
static const int invalidHistoryPos = -1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool isForbidden(const QString &input)
|
|
|
|
{
|
|
|
|
static const QRegExp delimiters("[;&]");
|
|
|
|
|
|
|
|
|
|
|
|
const QStringList &commands = input.split(delimiters, QString::SkipEmptyParts);
|
|
|
|
|
|
|
|
for (const QString &command : commands)
|
|
|
|
{
|
|
|
|
const QString &trimmedCommand = command.trimmed();
|
|
|
|
|
|
|
|
if (forbiddenArgs.contains(trimmedCommand)) return true;
|
|
|
|
|
|
|
|
for (const QString &arg : forbiddenArgs)
|
|
|
|
{
|
|
|
|
if (trimmedCommand.startsWith(arg + " ")) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-26 22:56:19 +00:00
|
|
|
|
|
|
|
|
2017-10-03 18:38:34 +00:00
|
|
|
ConsoleWidget::ConsoleWidget(MainWindow *main, QWidget *parent) :
|
2017-04-26 22:56:19 +00:00
|
|
|
QWidget(parent),
|
|
|
|
ui(new Ui::ConsoleWidget),
|
2017-10-03 18:38:34 +00:00
|
|
|
core(main->core),
|
|
|
|
main(main),
|
2017-04-27 23:57:13 +00:00
|
|
|
debugOutputEnabled(true),
|
|
|
|
maxHistoryEntries(100),
|
|
|
|
lastHistoryPosition(invalidHistoryPos)
|
2017-04-26 22:56:19 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
// Adjust console lineedit
|
2017-04-27 23:57:13 +00:00
|
|
|
ui->inputLineEdit->setTextMargins(10, 0, 0, 0);
|
2017-04-26 22:56:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
ui->consoleOutputTextEdit->setFont(QFont("Monospace", 8));
|
|
|
|
ui->consoleOutputTextEdit->setStyleSheet("background-color:black;color:gray;");
|
|
|
|
ui->consoleInputLineEdit->setStyleSheet("background-color:black;color:gray;");
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Adjust text margins of consoleOutputTextEdit
|
2017-04-27 23:57:13 +00:00
|
|
|
QTextDocument *console_docu = ui->outputTextEdit->document();
|
2017-04-26 22:56:19 +00:00
|
|
|
console_docu->setDocumentMargin(10);
|
|
|
|
|
|
|
|
// Fix output panel font
|
2017-04-27 23:57:13 +00:00
|
|
|
qhelpers::normalizeFont(ui->outputTextEdit);
|
|
|
|
|
|
|
|
QAction *action = new QAction(tr("Clear ouput"), ui->outputTextEdit);
|
|
|
|
connect(action, SIGNAL(triggered(bool)), ui->outputTextEdit, SLOT(clear()));
|
|
|
|
actions.append(action);
|
|
|
|
|
|
|
|
action = new QAction(tr("Sync with core"), ui->outputTextEdit);
|
|
|
|
action->setCheckable(true);
|
|
|
|
connect(action, SIGNAL(toggled(bool)), this, SLOT(syncWithCoreToggled(bool)));
|
|
|
|
actions.append(action);
|
|
|
|
|
|
|
|
// Completion
|
|
|
|
QCompleter *completer = new QCompleter(radareArgs, this);
|
|
|
|
completer->setMaxVisibleItems(20);
|
|
|
|
completer->setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
completer->setFilterMode(Qt::MatchStartsWith);
|
|
|
|
|
|
|
|
ui->inputLineEdit->setCompleter(completer);
|
2017-04-26 22:56:19 +00:00
|
|
|
|
|
|
|
// Set console output context menu
|
2017-04-27 23:57:13 +00:00
|
|
|
ui->outputTextEdit->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
connect(ui->outputTextEdit, SIGNAL(customContextMenuRequested(const QPoint &)),
|
|
|
|
this, SLOT(showCustomContextMenu(const QPoint &)));
|
|
|
|
|
|
|
|
// Esc clears inputLineEdit (like OmniBar)
|
|
|
|
QShortcut *clear_shortcut = new QShortcut(QKeySequence(Qt::Key_Escape), ui->inputLineEdit);
|
|
|
|
connect(clear_shortcut, SIGNAL(activated()), this, SLOT(clear()));
|
|
|
|
clear_shortcut->setContext(Qt::WidgetShortcut);
|
|
|
|
|
|
|
|
// Up and down arrows show history
|
|
|
|
QShortcut *historyOnUp = new QShortcut(QKeySequence(Qt::Key_Up), ui->inputLineEdit);
|
|
|
|
connect(historyOnUp, SIGNAL(activated()), this, SLOT(historyPrev()));
|
|
|
|
historyOnUp->setContext(Qt::WidgetShortcut);
|
|
|
|
|
|
|
|
QShortcut *historyOnDown = new QShortcut(QKeySequence(Qt::Key_Down), ui->inputLineEdit);
|
|
|
|
connect(historyOnDown, SIGNAL(activated()), this, SLOT(historyNext()));
|
|
|
|
historyOnDown->setContext(Qt::WidgetShortcut);
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 09:41:28 +00:00
|
|
|
ConsoleWidget::~ConsoleWidget() {}
|
2017-04-26 22:56:19 +00:00
|
|
|
|
|
|
|
void ConsoleWidget::addOutput(const QString &msg)
|
|
|
|
{
|
2017-04-27 23:57:13 +00:00
|
|
|
ui->outputTextEdit->appendPlainText(msg);
|
|
|
|
scrollOutputToEnd();
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleWidget::addDebugOutput(const QString &msg)
|
|
|
|
{
|
2017-04-27 23:57:13 +00:00
|
|
|
if (debugOutputEnabled)
|
|
|
|
{
|
|
|
|
ui->outputTextEdit->appendHtml("<font color=\"red\"> [DEBUG]:\t" + msg + "</font>");
|
|
|
|
scrollOutputToEnd();
|
|
|
|
}
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleWidget::focusInputLineEdit()
|
|
|
|
{
|
2017-04-27 23:57:13 +00:00
|
|
|
ui->inputLineEdit->setFocus();
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 18:38:34 +00:00
|
|
|
QString ConsoleWidget::executeCommand(QString command)
|
|
|
|
{
|
|
|
|
RVA offset = this->core->getOffset();
|
|
|
|
QString res = this->core->cmd(command);
|
|
|
|
RVA newOffset = this->core->getOffset();
|
|
|
|
if (offset != newOffset) {
|
|
|
|
emit main->seekChanged(newOffset);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-04-27 23:57:13 +00:00
|
|
|
void ConsoleWidget::on_inputLineEdit_returnPressed()
|
2017-04-26 22:56:19 +00:00
|
|
|
{
|
2017-04-27 23:57:13 +00:00
|
|
|
QString input = ui->inputLineEdit->text();
|
|
|
|
if (!input.isEmpty() && core != nullptr)
|
2017-04-26 22:56:19 +00:00
|
|
|
{
|
2017-09-29 11:32:53 +00:00
|
|
|
if (!isForbidden(input))
|
2017-04-26 22:56:19 +00:00
|
|
|
{
|
2017-10-03 18:38:34 +00:00
|
|
|
QString res = executeCommand(input);
|
|
|
|
ui->outputTextEdit->appendPlainText(res);
|
2017-04-27 23:57:13 +00:00
|
|
|
scrollOutputToEnd();
|
|
|
|
historyAdd(input);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addDebugOutput(tr("command forbidden: ") + input);
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 23:57:13 +00:00
|
|
|
ui->inputLineEdit->clear();
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 23:57:13 +00:00
|
|
|
void ConsoleWidget::on_execButton_clicked()
|
2017-04-26 22:56:19 +00:00
|
|
|
{
|
2017-04-27 23:57:13 +00:00
|
|
|
on_inputLineEdit_returnPressed();
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 23:57:13 +00:00
|
|
|
void ConsoleWidget::showCustomContextMenu(const QPoint &pt)
|
2017-04-26 22:56:19 +00:00
|
|
|
{
|
2017-04-27 23:57:13 +00:00
|
|
|
QMenu *menu = new QMenu(ui->outputTextEdit);
|
|
|
|
menu->addActions(actions);
|
|
|
|
menu->exec(ui->outputTextEdit->mapToGlobal(pt));
|
|
|
|
menu->deleteLater();
|
|
|
|
}
|
2017-04-26 22:56:19 +00:00
|
|
|
|
2017-04-27 23:57:13 +00:00
|
|
|
void ConsoleWidget::syncWithCoreToggled(bool checked)
|
|
|
|
{
|
2017-10-03 18:38:34 +00:00
|
|
|
// TODO Core and Cutter are always in sync
|
2017-04-27 23:57:13 +00:00
|
|
|
if (checked)
|
|
|
|
{
|
|
|
|
//Enable core syncronization
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Disable core sync
|
|
|
|
}
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 23:57:13 +00:00
|
|
|
void ConsoleWidget::historyNext()
|
2017-04-26 22:56:19 +00:00
|
|
|
{
|
2017-04-27 23:57:13 +00:00
|
|
|
if (!history.isEmpty())
|
|
|
|
{
|
|
|
|
if (lastHistoryPosition > invalidHistoryPos)
|
|
|
|
{
|
|
|
|
if (lastHistoryPosition >= history.size())
|
|
|
|
{
|
2017-06-03 12:27:23 +00:00
|
|
|
lastHistoryPosition = history.size() - 1 ;
|
2017-04-27 23:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
--lastHistoryPosition;
|
|
|
|
|
|
|
|
if (lastHistoryPosition >= 0)
|
|
|
|
{
|
|
|
|
ui->inputLineEdit->setText(history.at(lastHistoryPosition));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->inputLineEdit->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 23:57:13 +00:00
|
|
|
void ConsoleWidget::historyPrev()
|
2017-04-26 22:56:19 +00:00
|
|
|
{
|
2017-04-27 23:57:13 +00:00
|
|
|
if (!history.isEmpty())
|
|
|
|
{
|
2017-06-03 12:27:23 +00:00
|
|
|
if (lastHistoryPosition >= history.size() - 1)
|
2017-04-27 23:57:13 +00:00
|
|
|
{
|
|
|
|
lastHistoryPosition = history.size() - 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->inputLineEdit->setText(history.at(++lastHistoryPosition));
|
|
|
|
}
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 23:57:13 +00:00
|
|
|
void ConsoleWidget::clear()
|
|
|
|
{
|
|
|
|
ui->inputLineEdit->clear();
|
|
|
|
|
|
|
|
invalidateHistoryPosition();
|
2017-04-26 22:56:19 +00:00
|
|
|
|
2017-04-27 23:57:13 +00:00
|
|
|
// Close the potential shown completer popup
|
|
|
|
ui->inputLineEdit->clearFocus();
|
|
|
|
ui->inputLineEdit->setFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleWidget::scrollOutputToEnd()
|
2017-04-26 22:56:19 +00:00
|
|
|
{
|
2017-04-27 23:57:13 +00:00
|
|
|
const int maxValue = ui->outputTextEdit->verticalScrollBar()->maximum();
|
|
|
|
ui->outputTextEdit->verticalScrollBar()->setValue(maxValue);
|
|
|
|
}
|
2017-04-26 22:56:19 +00:00
|
|
|
|
2017-04-27 23:57:13 +00:00
|
|
|
void ConsoleWidget::historyAdd(const QString &input)
|
|
|
|
{
|
|
|
|
if (history.size() + 1 > maxHistoryEntries)
|
2017-04-26 22:56:19 +00:00
|
|
|
{
|
2017-04-27 23:57:13 +00:00
|
|
|
history.removeLast();
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
2017-04-27 23:57:13 +00:00
|
|
|
|
|
|
|
history.prepend(input);
|
|
|
|
|
|
|
|
invalidateHistoryPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleWidget::invalidateHistoryPosition()
|
|
|
|
{
|
|
|
|
lastHistoryPosition = invalidHistoryPos;
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|