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>
|
2018-10-10 18:09:07 +00:00
|
|
|
#include <QTimer>
|
2019-05-17 17:48:59 +00:00
|
|
|
#include <QSettings>
|
2019-12-04 12:27:38 +00:00
|
|
|
#include <QDir>
|
|
|
|
#include <QUuid>
|
2018-10-21 14:53:38 +00:00
|
|
|
#include <iostream>
|
2019-02-22 16:50:45 +00:00
|
|
|
#include "core/Cutter.h"
|
2017-10-02 16:18:40 +00:00
|
|
|
#include "ConsoleWidget.h"
|
|
|
|
#include "ui_ConsoleWidget.h"
|
2018-10-17 07:55:53 +00:00
|
|
|
#include "common/Helpers.h"
|
|
|
|
#include "common/SvgIconEngine.h"
|
2017-04-26 22:56:19 +00:00
|
|
|
|
2019-10-14 10:00:44 +00:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#include <io.h>
|
|
|
|
#define dup2 _dup2
|
|
|
|
#define dup _dup
|
|
|
|
#define fileno _fileno
|
|
|
|
#define fdopen _fdopen
|
|
|
|
#define PIPE_SIZE 65536 // Match Linux size
|
|
|
|
#define PIPE_NAME "\\\\.\\pipe\\cutteroutput-%1"
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#define PIPE_READ (0)
|
|
|
|
#define PIPE_WRITE (1)
|
2019-12-04 12:27:38 +00:00
|
|
|
#define STDIN_PIPE_NAME "%1/cutter-stdin-%2"
|
2019-10-14 10:00:44 +00:00
|
|
|
#endif
|
2017-04-26 22:56:19 +00:00
|
|
|
|
2019-12-04 12:27:38 +00:00
|
|
|
#define CONSOLE_R2_INPUT ("R2 Console")
|
|
|
|
#define CONSOLE_DEBUGEE_INPUT ("Debugee Input")
|
|
|
|
|
2017-04-27 23:57:13 +00:00
|
|
|
static const int invalidHistoryPos = -1;
|
|
|
|
|
2019-05-17 17:48:59 +00:00
|
|
|
static const char *consoleWrapSettingsKey = "console.wrap";
|
2017-04-27 23:57:13 +00:00
|
|
|
|
2018-03-16 21:46:57 +00:00
|
|
|
ConsoleWidget::ConsoleWidget(MainWindow *main, QAction *action) :
|
|
|
|
CutterDockWidget(main, action),
|
2017-04-26 22:56:19 +00:00
|
|
|
ui(new Ui::ConsoleWidget),
|
2017-04-27 23:57:13 +00:00
|
|
|
debugOutputEnabled(true),
|
|
|
|
maxHistoryEntries(100),
|
2019-05-19 12:45:49 +00:00
|
|
|
lastHistoryPosition(invalidHistoryPos),
|
|
|
|
completer(nullptr),
|
|
|
|
historyUpShortcut(nullptr),
|
|
|
|
historyDownShortcut(nullptr)
|
2017-04-26 22:56:19 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
// Adjust console lineedit
|
2019-12-04 12:27:38 +00:00
|
|
|
ui->r2InputLineEdit->setTextMargins(10, 0, 0, 0);
|
|
|
|
ui->debugeeInputLineEdit->setTextMargins(10, 0, 0, 0);
|
2017-04-26 22:56:19 +00:00
|
|
|
|
2017-11-26 21:54:44 +00:00
|
|
|
setupFont();
|
2017-04-26 22:56:19 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2018-03-16 21:46:57 +00:00
|
|
|
QAction *actionClear = new QAction(tr("Clear Output"), ui->outputTextEdit);
|
|
|
|
connect(actionClear, SIGNAL(triggered(bool)), ui->outputTextEdit, SLOT(clear()));
|
|
|
|
actions.append(actionClear);
|
2017-04-27 23:57:13 +00:00
|
|
|
|
2019-04-18 16:42:27 +00:00
|
|
|
actionWrapLines = new QAction(tr("Wrap Lines"), ui->outputTextEdit);
|
|
|
|
actionWrapLines->setCheckable(true);
|
2019-05-17 17:48:59 +00:00
|
|
|
setWrap(QSettings().value(consoleWrapSettingsKey, true).toBool());
|
2019-04-18 16:42:27 +00:00
|
|
|
connect(actionWrapLines, &QAction::triggered, this, [this] (bool checked) {
|
2019-05-17 17:48:59 +00:00
|
|
|
setWrap(checked);
|
2019-04-18 16:42:27 +00:00
|
|
|
});
|
|
|
|
actions.append(actionWrapLines);
|
|
|
|
|
2017-04-27 23:57:13 +00:00
|
|
|
// Completion
|
2019-05-19 14:24:06 +00:00
|
|
|
completionActive = false;
|
2019-04-26 15:07:11 +00:00
|
|
|
completer = new QCompleter(&completionModel, this);
|
2017-04-27 23:57:13 +00:00
|
|
|
completer->setMaxVisibleItems(20);
|
|
|
|
completer->setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
completer->setFilterMode(Qt::MatchStartsWith);
|
2019-12-04 12:27:38 +00:00
|
|
|
ui->r2InputLineEdit->setCompleter(completer);
|
2017-04-26 22:56:19 +00:00
|
|
|
|
2019-12-04 12:27:38 +00:00
|
|
|
connect(ui->r2InputLineEdit, &QLineEdit::textEdited, this, &ConsoleWidget::updateCompletion);
|
2019-04-26 15:07:11 +00:00
|
|
|
updateCompletion();
|
|
|
|
|
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 &)));
|
|
|
|
|
2019-12-04 12:27:38 +00:00
|
|
|
// Esc clears r2InputLineEdit and debugeeInputLineEdit (like OmniBar)
|
|
|
|
QShortcut *r2_clear_shortcut = new QShortcut(QKeySequence(Qt::Key_Escape), ui->r2InputLineEdit);
|
|
|
|
connect(r2_clear_shortcut, SIGNAL(activated()), this, SLOT(clear()));
|
|
|
|
r2_clear_shortcut->setContext(Qt::WidgetShortcut);
|
|
|
|
|
|
|
|
QShortcut *debugee_clear_shortcut = new QShortcut(QKeySequence(Qt::Key_Escape), ui->debugeeInputLineEdit);
|
|
|
|
connect(debugee_clear_shortcut, SIGNAL(activated()), this, SLOT(clear()));
|
|
|
|
debugee_clear_shortcut->setContext(Qt::WidgetShortcut);
|
2017-04-27 23:57:13 +00:00
|
|
|
|
|
|
|
// Up and down arrows show history
|
2019-12-04 12:27:38 +00:00
|
|
|
historyUpShortcut = new QShortcut(QKeySequence(Qt::Key_Up), ui->r2InputLineEdit);
|
2019-05-19 12:45:49 +00:00
|
|
|
connect(historyUpShortcut, SIGNAL(activated()), this, SLOT(historyPrev()));
|
|
|
|
historyUpShortcut->setContext(Qt::WidgetShortcut);
|
2017-04-27 23:57:13 +00:00
|
|
|
|
2019-12-04 12:27:38 +00:00
|
|
|
historyDownShortcut = new QShortcut(QKeySequence(Qt::Key_Down), ui->r2InputLineEdit);
|
2019-05-19 12:45:49 +00:00
|
|
|
connect(historyDownShortcut, SIGNAL(activated()), this, SLOT(historyNext()));
|
|
|
|
historyDownShortcut->setContext(Qt::WidgetShortcut);
|
2017-11-26 21:54:44 +00:00
|
|
|
|
2019-12-04 12:27:38 +00:00
|
|
|
QShortcut *completionShortcut = new QShortcut(QKeySequence(Qt::Key_Tab), ui->r2InputLineEdit);
|
2019-05-19 14:24:06 +00:00
|
|
|
connect(completionShortcut, &QShortcut::activated, this, &ConsoleWidget::triggerCompletion);
|
|
|
|
|
2019-12-04 12:27:38 +00:00
|
|
|
connect(ui->r2InputLineEdit, &QLineEdit::editingFinished, this, &ConsoleWidget::disableCompletion);
|
2019-05-19 14:24:06 +00:00
|
|
|
|
2019-05-20 19:06:25 +00:00
|
|
|
connect(Config(), &Configuration::fontsUpdated, this, &ConsoleWidget::setupFont);
|
2019-05-19 12:45:49 +00:00
|
|
|
|
2019-12-04 12:27:38 +00:00
|
|
|
connect(ui->inputCombo,
|
|
|
|
static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
|
|
|
|
this, &ConsoleWidget::onIndexChange);
|
|
|
|
|
|
|
|
connect(Core(), &CutterCore::debugTaskStateChanged, this, [ = ]() {
|
|
|
|
if (Core()->isRedirectableDebugee()) {
|
|
|
|
ui->inputCombo->setVisible(true);
|
|
|
|
} else {
|
|
|
|
ui->inputCombo->setVisible(false);
|
|
|
|
// Return to the r2 console
|
|
|
|
ui->inputCombo->setCurrentIndex(ui->inputCombo->findText(CONSOLE_R2_INPUT));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-19 12:45:49 +00:00
|
|
|
completer->popup()->installEventFilter(this);
|
2019-10-14 10:00:44 +00:00
|
|
|
|
2020-04-17 12:02:44 +00:00
|
|
|
if (Config()->getOutputRedirectionEnabled()) {
|
|
|
|
redirectOutput();
|
|
|
|
}
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 15:07:11 +00:00
|
|
|
ConsoleWidget::~ConsoleWidget()
|
|
|
|
{
|
2019-12-04 12:27:38 +00:00
|
|
|
#ifndef Q_OS_WIN
|
|
|
|
::close(stdinFile);
|
|
|
|
remove(stdinFifoPath.toStdString().c_str());
|
|
|
|
#endif
|
2019-04-26 15:07:11 +00:00
|
|
|
}
|
2017-04-26 22:56:19 +00:00
|
|
|
|
2019-05-19 12:45:49 +00:00
|
|
|
bool ConsoleWidget::eventFilter(QObject *obj, QEvent *event)
|
|
|
|
{
|
|
|
|
if(completer && obj == completer->popup() &&
|
|
|
|
// disable up/down shortcuts if completer is shown
|
|
|
|
(event->type() == QEvent::Type::Show || event->type() == QEvent::Type::Hide)) {
|
|
|
|
bool enabled = !completer->popup()->isVisible();
|
|
|
|
if (historyUpShortcut) {
|
|
|
|
historyUpShortcut->setEnabled(enabled);
|
|
|
|
}
|
|
|
|
if (historyDownShortcut) {
|
|
|
|
historyDownShortcut->setEnabled(enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-26 21:54:44 +00:00
|
|
|
void ConsoleWidget::setupFont()
|
|
|
|
{
|
|
|
|
ui->outputTextEdit->setFont(Config()->getFont());
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2018-03-21 20:32:32 +00:00
|
|
|
if (debugOutputEnabled) {
|
2017-04-27 23:57:13 +00:00
|
|
|
ui->outputTextEdit->appendHtml("<font color=\"red\"> [DEBUG]:\t" + msg + "</font>");
|
|
|
|
scrollOutputToEnd();
|
|
|
|
}
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleWidget::focusInputLineEdit()
|
|
|
|
{
|
2019-12-04 12:27:38 +00:00
|
|
|
ui->r2InputLineEdit->setFocus();
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 18:09:07 +00:00
|
|
|
void ConsoleWidget::removeLastLine()
|
|
|
|
{
|
|
|
|
ui->outputTextEdit->setFocus();
|
|
|
|
QTextCursor cur = ui->outputTextEdit->textCursor();
|
|
|
|
ui->outputTextEdit->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
|
|
|
|
ui->outputTextEdit->moveCursor(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
|
|
|
|
ui->outputTextEdit->moveCursor(QTextCursor::End, QTextCursor::KeepAnchor);
|
|
|
|
ui->outputTextEdit->textCursor().removeSelectedText();
|
|
|
|
ui->outputTextEdit->textCursor().deletePreviousChar();
|
|
|
|
ui->outputTextEdit->setTextCursor(cur);
|
|
|
|
}
|
|
|
|
|
2018-06-22 18:34:25 +00:00
|
|
|
void ConsoleWidget::executeCommand(const QString &command)
|
|
|
|
{
|
|
|
|
if (!commandTask.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-12-04 12:27:38 +00:00
|
|
|
ui->r2InputLineEdit->setEnabled(false);
|
2018-06-24 19:16:57 +00:00
|
|
|
|
2019-10-14 10:00:44 +00:00
|
|
|
QString cmd_line = "[" + RAddressString(Core()->getOffset()) + "]> " + command;
|
|
|
|
addOutput(cmd_line);
|
2018-06-22 18:34:25 +00:00
|
|
|
|
2018-11-11 11:10:32 +00:00
|
|
|
RVA oldOffset = Core()->getOffset();
|
2018-10-21 14:53:38 +00:00
|
|
|
commandTask = QSharedPointer<CommandTask>(new CommandTask(command, CommandTask::ColorMode::MODE_256, true));
|
2018-09-30 20:00:53 +00:00
|
|
|
connect(commandTask.data(), &CommandTask::finished, this, [this, cmd_line,
|
2019-10-14 10:00:44 +00:00
|
|
|
command, oldOffset] (const QString & result) {
|
2018-10-21 14:53:38 +00:00
|
|
|
|
2019-10-14 10:00:44 +00:00
|
|
|
ui->outputTextEdit->appendHtml(result);
|
2018-06-22 18:34:25 +00:00
|
|
|
scrollOutputToEnd();
|
|
|
|
historyAdd(command);
|
2019-10-06 17:35:44 +00:00
|
|
|
commandTask.clear();
|
2019-12-04 12:27:38 +00:00
|
|
|
ui->r2InputLineEdit->setEnabled(true);
|
|
|
|
ui->r2InputLineEdit->setFocus();
|
2019-10-14 10:00:44 +00:00
|
|
|
|
2018-11-11 11:10:32 +00:00
|
|
|
if (oldOffset != Core()->getOffset()) {
|
|
|
|
Core()->updateSeek();
|
|
|
|
}
|
2018-06-22 18:34:25 +00:00
|
|
|
});
|
2018-10-10 18:09:07 +00:00
|
|
|
|
2018-06-22 18:34:25 +00:00
|
|
|
Core()->getAsyncTaskManager()->start(commandTask);
|
|
|
|
}
|
|
|
|
|
2019-12-04 12:27:38 +00:00
|
|
|
void ConsoleWidget::sendToStdin(const QString &input)
|
|
|
|
{
|
|
|
|
#ifndef Q_OS_WIN
|
|
|
|
write(stdinFile, (input + "\n").toStdString().c_str(), input.size() + 1);
|
|
|
|
fsync(stdinFile);
|
|
|
|
addOutput("Sent input: '" + input + "'");
|
|
|
|
#else
|
|
|
|
// Stdin redirection isn't currently available in windows because console applications
|
|
|
|
// with stdin already get their own console window with stdin when they are launched
|
|
|
|
// that the user can type into.
|
|
|
|
addOutput("Unsupported feature");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleWidget::onIndexChange()
|
|
|
|
{
|
|
|
|
QString console = ui->inputCombo->currentText();
|
|
|
|
if (console == CONSOLE_DEBUGEE_INPUT) {
|
|
|
|
ui->r2InputLineEdit->setVisible(false);
|
|
|
|
ui->debugeeInputLineEdit->setVisible(true);
|
|
|
|
} else if (console == CONSOLE_R2_INPUT) {
|
|
|
|
ui->r2InputLineEdit->setVisible(true);
|
|
|
|
ui->debugeeInputLineEdit->setVisible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 17:48:59 +00:00
|
|
|
void ConsoleWidget::setWrap(bool wrap)
|
|
|
|
{
|
|
|
|
QSettings().setValue(consoleWrapSettingsKey, wrap);
|
|
|
|
actionWrapLines->setChecked(wrap);
|
|
|
|
ui->outputTextEdit->setLineWrapMode(wrap ? QPlainTextEdit::WidgetWidth: QPlainTextEdit::NoWrap);
|
|
|
|
}
|
|
|
|
|
2019-12-04 12:27:38 +00:00
|
|
|
void ConsoleWidget::on_r2InputLineEdit_returnPressed()
|
2017-04-26 22:56:19 +00:00
|
|
|
{
|
2019-12-04 12:27:38 +00:00
|
|
|
QString input = ui->r2InputLineEdit->text();
|
2018-10-13 13:44:20 +00:00
|
|
|
if (input.isEmpty()) {
|
|
|
|
return;
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
2018-10-13 13:44:20 +00:00
|
|
|
executeCommand(input);
|
2019-12-04 12:27:38 +00:00
|
|
|
ui->r2InputLineEdit->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleWidget::on_debugeeInputLineEdit_returnPressed()
|
|
|
|
{
|
|
|
|
QString input = ui->debugeeInputLineEdit->text();
|
|
|
|
if (input.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sendToStdin(input);
|
|
|
|
ui->debugeeInputLineEdit->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
|
|
|
{
|
2019-12-04 12:27:38 +00:00
|
|
|
on_r2InputLineEdit_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
|
|
|
{
|
2019-04-18 16:42:27 +00:00
|
|
|
actionWrapLines->setChecked(ui->outputTextEdit->lineWrapMode() == QPlainTextEdit::WidgetWidth);
|
|
|
|
|
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::historyNext()
|
2017-04-26 22:56:19 +00:00
|
|
|
{
|
2018-03-21 20:32:32 +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;
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
if (lastHistoryPosition >= 0) {
|
2019-12-04 12:27:38 +00:00
|
|
|
ui->r2InputLineEdit->setText(history.at(lastHistoryPosition));
|
2018-03-21 20:32:32 +00:00
|
|
|
} else {
|
2019-12-04 12:27:38 +00:00
|
|
|
ui->r2InputLineEdit->clear();
|
2017-04-27 23:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2018-03-21 20:32:32 +00:00
|
|
|
if (!history.isEmpty()) {
|
|
|
|
if (lastHistoryPosition >= history.size() - 1) {
|
2017-04-27 23:57:13 +00:00
|
|
|
lastHistoryPosition = history.size() - 2;
|
|
|
|
}
|
|
|
|
|
2019-12-04 12:27:38 +00:00
|
|
|
ui->r2InputLineEdit->setText(history.at(++lastHistoryPosition));
|
2017-04-27 23:57:13 +00:00
|
|
|
}
|
2017-04-26 22:56:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 14:24:06 +00:00
|
|
|
void ConsoleWidget::triggerCompletion()
|
|
|
|
{
|
|
|
|
if (completionActive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
completionActive = true;
|
|
|
|
updateCompletion();
|
|
|
|
completer->complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleWidget::disableCompletion()
|
|
|
|
{
|
|
|
|
if (!completionActive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
completionActive = false;
|
|
|
|
updateCompletion();
|
|
|
|
completer->popup()->hide();
|
|
|
|
}
|
|
|
|
|
2019-04-26 15:07:11 +00:00
|
|
|
void ConsoleWidget::updateCompletion()
|
|
|
|
{
|
2019-05-19 14:24:06 +00:00
|
|
|
if (!completionActive) {
|
|
|
|
completionModel.setStringList({});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-04 12:27:38 +00:00
|
|
|
auto current = ui->r2InputLineEdit->text();
|
2019-04-26 15:07:11 +00:00
|
|
|
auto completions = Core()->autocomplete(current, R_LINE_PROMPT_DEFAULT);
|
|
|
|
int lastSpace = current.lastIndexOf(' ');
|
|
|
|
if (lastSpace >= 0) {
|
|
|
|
current = current.left(lastSpace + 1);
|
|
|
|
for (auto &s : completions) {
|
|
|
|
s = current + s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
completionModel.setStringList(completions);
|
|
|
|
}
|
|
|
|
|
2017-04-27 23:57:13 +00:00
|
|
|
void ConsoleWidget::clear()
|
|
|
|
{
|
2019-05-19 14:24:06 +00:00
|
|
|
disableCompletion();
|
2019-12-04 12:27:38 +00:00
|
|
|
ui->r2InputLineEdit->clear();
|
|
|
|
ui->debugeeInputLineEdit->clear();
|
2017-04-27 23:57:13 +00:00
|
|
|
|
|
|
|
invalidateHistoryPosition();
|
2017-04-26 22:56:19 +00:00
|
|
|
|
2017-04-27 23:57:13 +00:00
|
|
|
// Close the potential shown completer popup
|
2019-12-04 12:27:38 +00:00
|
|
|
ui->r2InputLineEdit->clearFocus();
|
|
|
|
ui->r2InputLineEdit->setFocus();
|
2017-04-27 23:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2018-03-21 20:32:32 +00:00
|
|
|
if (history.size() + 1 > maxHistoryEntries) {
|
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
|
|
|
}
|
2019-10-14 10:00:44 +00:00
|
|
|
|
|
|
|
void ConsoleWidget::processQueuedOutput()
|
|
|
|
{
|
|
|
|
// Partial lines are ignored since carriage return is currently unsupported
|
|
|
|
while (pipeSocket->canReadLine()) {
|
|
|
|
QString output = QString(pipeSocket->readLine());
|
|
|
|
|
|
|
|
fprintf(origStderr, "%s", output.toStdString().c_str());
|
|
|
|
|
|
|
|
// Get the last segment that wasn't overwritten by carriage return
|
|
|
|
output = output.trimmed();
|
|
|
|
output = output.remove(0, output.lastIndexOf('\r')).trimmed();
|
|
|
|
ui->outputTextEdit->appendHtml(CutterCore::ansiEscapeToHtml(output));
|
|
|
|
scrollOutputToEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleWidget::redirectOutput()
|
|
|
|
{
|
|
|
|
// Make sure that we are running in a valid console with initialized output handles
|
|
|
|
if (0 > fileno(stderr) && 0 > fileno(stdout)) {
|
|
|
|
addOutput("Run cutter in a console to enable r2 output redirection into this widget.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipeSocket = new QLocalSocket(this);
|
|
|
|
|
2019-12-04 12:27:38 +00:00
|
|
|
origStdin = fdopen(dup(fileno(stderr)), "r");
|
2019-10-14 10:00:44 +00:00
|
|
|
origStderr = fdopen(dup(fileno(stderr)), "a");
|
|
|
|
origStdout = fdopen(dup(fileno(stdout)), "a");
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
QString pipeName = QString::fromLatin1(PIPE_NAME).arg(QUuid::createUuid().toString());
|
|
|
|
|
|
|
|
SECURITY_ATTRIBUTES attributes = {sizeof(SECURITY_ATTRIBUTES), 0, false};
|
2019-12-04 12:27:38 +00:00
|
|
|
hWrite = CreateNamedPipeW((wchar_t *)pipeName.utf16(), PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
|
2019-10-14 10:00:44 +00:00
|
|
|
PIPE_TYPE_BYTE | PIPE_WAIT, 1, PIPE_SIZE, PIPE_SIZE, 0, &attributes);
|
2019-12-04 12:27:38 +00:00
|
|
|
|
2019-10-14 10:00:44 +00:00
|
|
|
int writeFd = _open_osfhandle((intptr_t)hWrite, _O_WRONLY | _O_TEXT);
|
|
|
|
dup2(writeFd, fileno(stdout));
|
|
|
|
dup2(writeFd, fileno(stderr));
|
|
|
|
|
|
|
|
pipeSocket->connectToServer(pipeName, QIODevice::ReadOnly);
|
|
|
|
#else
|
|
|
|
pipe(redirectPipeFds);
|
2019-12-04 12:27:38 +00:00
|
|
|
stdinFifoPath = QString(STDIN_PIPE_NAME).arg(QDir::tempPath(), QUuid::createUuid().toString());
|
|
|
|
mkfifo(stdinFifoPath.toStdString().c_str(), (mode_t) 0777);
|
|
|
|
stdinFile = open(stdinFifoPath.toStdString().c_str(), O_RDWR | O_ASYNC);
|
|
|
|
|
|
|
|
dup2(stdinFile, fileno(stdin));
|
2019-10-14 10:00:44 +00:00
|
|
|
dup2(redirectPipeFds[PIPE_WRITE], fileno(stderr));
|
|
|
|
dup2(redirectPipeFds[PIPE_WRITE], fileno(stdout));
|
|
|
|
|
|
|
|
// Attempt to force line buffering to avoid calling processQueuedOutput
|
|
|
|
// for partial lines
|
|
|
|
setlinebuf(stderr);
|
|
|
|
setlinebuf(stdout);
|
|
|
|
|
|
|
|
// Configure the pipe to work in async mode
|
|
|
|
fcntl(redirectPipeFds[PIPE_READ], F_SETFL, O_ASYNC | O_NONBLOCK);
|
|
|
|
|
|
|
|
pipeSocket->setSocketDescriptor(redirectPipeFds[PIPE_READ]);
|
|
|
|
pipeSocket->connectToServer(QIODevice::ReadOnly);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
connect(pipeSocket, SIGNAL(readyRead()), this, SLOT(processQueuedOutput()));
|
|
|
|
}
|