From: Christoph Egger Date: Tue, 10 Mar 2015 16:22:38 +0000 (+0100) Subject: Move Interpreter to its own class X-Git-Tag: v0.1~54 X-Git-Url: https://git.siccegge.de//index.cgi?p=frida%2Ffrida.git;a=commitdiff_plain;h=7cc1f7b923b7859a7469e6a651d4a87bc48c4772 Move Interpreter to its own class Moving the actual scripting implementation from the ScriptingDock to its own class. Includes a guile implementation for the Interpreter class. Fixes: T4 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index c88c46a..5e5c9ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ separate_arguments(LLVM_LDFLAGS) separate_arguments(LLVM_LIBS) SET(frida_SOURCES + src/bindings/Guile.cxx src/core/InformationManager.cxx src/core/BasicBlock.cxx src/core/Function.cxx diff --git a/src/bindings/Guile.cxx b/src/bindings/Guile.cxx new file mode 100644 index 0000000..2552e7d --- /dev/null +++ b/src/bindings/Guile.cxx @@ -0,0 +1,52 @@ +#include "Guile.hxx" + +namespace { + SCM handler (void*, SCM tag, SCM throw_args) { + scm_handle_by_message_noexit ((void*)"foo", tag, throw_args); + return SCM_BOOL_F; + } +} + +GuileInterpreter::GuileInterpreter() { + scm_init_guile(); + + scm_internal_catch(SCM_BOOL_T, + (SCM (*)(void *))scm_c_eval_string, + (void*)"(use-modules (system repl server))", + handler, NULL); + scm_internal_catch(SCM_BOOL_T, + (SCM (*)(void *))scm_c_eval_string, + (void*)"(spawn-server)", + handler, NULL); + + guile_output_port = scm_open_output_string(); + guile_error_port = scm_open_output_string(); + scm_set_current_output_port(guile_output_port); + scm_set_current_error_port(guile_error_port); +} + +int GuileInterpreter::evaluate(const std::string& command, + std::ostream& stdout, + std::ostream& stderr, + std::string& result) { + + SCM result_obj = scm_internal_catch(SCM_BOOL_T, + (SCM (*)(void *))scm_c_eval_string, + (void*)command.c_str(), + handler, NULL); + + SCM result_str = scm_object_to_string(result_obj, SCM_UNDEFINED); + + SCM output = scm_get_output_string(guile_output_port); + stdout << scm_to_locale_string(output); + + output = scm_get_output_string(guile_error_port); + stderr << scm_to_locale_string(output); + + result = scm_to_locale_string(result_str); + + scm_truncate_file(guile_output_port, scm_from_uint16(0)); + scm_truncate_file(guile_error_port, scm_from_uint16(0)); + + return 0; +} diff --git a/src/bindings/Guile.hxx b/src/bindings/Guile.hxx new file mode 100644 index 0000000..5de6908 --- /dev/null +++ b/src/bindings/Guile.hxx @@ -0,0 +1,21 @@ +#ifndef INCLUDE__Guile_hxx_ +#define INCLUDE__Guile_hxx_ + +#include + +#include "Interpreter.hxx" + +class GuileInterpreter : public Interpreter { +public: + GuileInterpreter(); + + int evaluate(const std::string& command, + std::ostream& stdout, + std::ostream& stderr, + std::string& result); +private: + SCM guile_output_port; + SCM guile_error_port; +}; + +#endif /* INCLUDE__Guile_hxx_ */ diff --git a/src/bindings/Interpreter.hxx b/src/bindings/Interpreter.hxx new file mode 100644 index 0000000..a8d9ad9 --- /dev/null +++ b/src/bindings/Interpreter.hxx @@ -0,0 +1,16 @@ +#ifndef INCLUDE__Interpreter_hxx_ +#define INCLUDE__Interpreter_hxx_ + +#include +#include + +class Interpreter { +public: + virtual int evaluate(const std::string& command, + std::ostream& stdout, + std::ostream& stderr, + std::string& result) = 0; +private: +}; + +#endif /* INCLUDE__Interpreter_hxx_ */ diff --git a/src/gui/Mainwindow.cxx b/src/gui/Mainwindow.cxx index dfcdc76..a42d3f4 100644 --- a/src/gui/Mainwindow.cxx +++ b/src/gui/Mainwindow.cxx @@ -1,5 +1,6 @@ #include "Mainwindow.hxx" #include "qt.hxx" +#include "bindings/Guile.hxx" #include "disassembler/llvm/LLVMDisassembler.hxx" #include "core/Function.hxx" #include "core/BasicBlock.hxx" @@ -45,7 +46,7 @@ Mainwindow::Mainwindow(InformationManager* mgr) fileMenu->addSeparator(); fileMenu->addAction(exitAction); - scripting = new ScriptingDock(tr("Scripting"), this); + scripting = new ScriptingDock(new GuileInterpreter, tr("Scripting"), this); scripting->setAllowedAreas(Qt::BottomDockWidgetArea); addDockWidget(Qt::BottomDockWidgetArea, scripting); diff --git a/src/gui/widgets/ScriptingDock.cxx b/src/gui/widgets/ScriptingDock.cxx index 6600ee6..b334d71 100644 --- a/src/gui/widgets/ScriptingDock.cxx +++ b/src/gui/widgets/ScriptingDock.cxx @@ -1,15 +1,12 @@ #include "ScriptingDock.hxx" +#include "bindings/Interpreter.hxx" -namespace { - SCM handler (void*, SCM tag, SCM throw_args) { - scm_handle_by_message_noexit ((void*)"foo", tag, throw_args); - return SCM_BOOL_F; - } -} +#include -ScriptingDock::ScriptingDock(const QString& title, QWidget * parent) +ScriptingDock::ScriptingDock(Interpreter* interpreter, const QString& title, QWidget * parent) : QDockWidget(title, parent) - , logger(log4cxx::Logger::getLogger("ScriptingDock")) { + , logger(log4cxx::Logger::getLogger("ScriptingDock")) + , interpreter(interpreter) { QTabWidget * tab = new QTabWidget; QWidget * widget = new QWidget; widget->setLayout(layout = new QGridLayout); @@ -20,47 +17,28 @@ ScriptingDock::ScriptingDock(const QString& title, QWidget * parent) ((QDockWidget*)this)->setWidget(tab); connect(button, SIGNAL(released()), this, SLOT(doEvaluate())); connect(line, SIGNAL(returnPressed()), this, SLOT(doEvaluate())); - scm_init_guile(); - scm_internal_catch(SCM_BOOL_T, - (SCM (*)(void *))scm_c_eval_string, - (void*)"(use-modules (system repl server))", - handler, NULL); - scm_internal_catch(SCM_BOOL_T, - (SCM (*)(void *))scm_c_eval_string, - (void*)"(spawn-server)", - handler, NULL); - - guile_output_port = scm_open_output_string(); - guile_error_port = scm_open_output_string(); - scm_set_current_output_port(guile_output_port); - scm_set_current_error_port(guile_error_port); } void ScriptingDock::doEvaluate() { + std::stringstream stdout, stderr; + std::string result; + QString output; QString text = line->text(); + line->clear(); LOG4CXX_INFO(logger, "Evaluating String \"" << text.toStdString() << "\""); browser->append(QString("> ") + text); - SCM result_obj = scm_internal_catch(SCM_BOOL_T, - (SCM (*)(void *))scm_c_eval_string, - (void*)text.toStdString().c_str(), - handler, NULL); - SCM result_str = scm_object_to_string(result_obj, SCM_UNDEFINED); - - SCM output = scm_get_output_string(guile_output_port); - QString output_q = scm_to_locale_string(output); - if (output_q.endsWith("\n")) output_q.chop(1); - if (output_q != "") browser->append(output_q); + interpreter->evaluate(text.toStdString(), stdout, stderr, result); - output = scm_get_output_string(guile_error_port); - output_q = scm_to_locale_string(output); - if (output_q.endsWith("\n")) output_q.chop(1); - if (output_q != "") browser->append(output_q); + output = stdout.str().c_str(); + if (output.endsWith("\n")) output.chop(1); + if (output != "") browser->append(output); - scm_truncate_file(guile_output_port, scm_from_uint16(0)); - scm_truncate_file(guile_error_port, scm_from_uint16(0)); + output = stderr.str().c_str(); + if (output.endsWith("\n")) output.chop(1); + if (output != "") browser->append(output); - browser->append(scm_to_locale_string(result_str)); + browser->append(result.c_str()); } diff --git a/src/gui/widgets/ScriptingDock.hxx b/src/gui/widgets/ScriptingDock.hxx index c02ec81..a49dc3e 100644 --- a/src/gui/widgets/ScriptingDock.hxx +++ b/src/gui/widgets/ScriptingDock.hxx @@ -4,11 +4,12 @@ #include #include +class Interpreter; + class ScriptingDock : public QDockWidget { Q_OBJECT - public: - ScriptingDock(const QString& title, QWidget * parent = 0); + ScriptingDock(Interpreter* interpreter, const QString& title, QWidget * parent = 0); private: log4cxx::LoggerPtr logger; @@ -18,8 +19,7 @@ private: QPushButton * button; QLineEdit * line; - SCM guile_output_port; - SCM guile_error_port; + Interpreter* interpreter; private Q_SLOTS: void doEvaluate(); };