]> git.siccegge.de Git - frida/frida.git/commitdiff
Move Interpreter to its own class
authorChristoph Egger <Christoph.Egger@fau.de>
Tue, 10 Mar 2015 16:22:38 +0000 (17:22 +0100)
committerChristoph Egger <Christoph.Egger@fau.de>
Tue, 10 Mar 2015 16:22:38 +0000 (17:22 +0100)
Moving the actual scripting implementation from the ScriptingDock to its
own class. Includes a guile implementation for the Interpreter class.

Fixes: T4
CMakeLists.txt
src/bindings/Guile.cxx [new file with mode: 0644]
src/bindings/Guile.hxx [new file with mode: 0644]
src/bindings/Interpreter.hxx [new file with mode: 0644]
src/gui/Mainwindow.cxx
src/gui/widgets/ScriptingDock.cxx
src/gui/widgets/ScriptingDock.hxx

index c88c46a75ab99240fe2f7aeb034c679e186ecf1c..5e5c9eac9d42cda6789ef6055c2ada4fa49815a4 100644 (file)
@@ -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 (file)
index 0000000..2552e7d
--- /dev/null
@@ -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 (file)
index 0000000..5de6908
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef INCLUDE__Guile_hxx_
+#define INCLUDE__Guile_hxx_
+
+#include <libguile.h>
+
+#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 (file)
index 0000000..a8d9ad9
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef INCLUDE__Interpreter_hxx_
+#define INCLUDE__Interpreter_hxx_
+
+#include <string>
+#include <sstream>
+
+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_ */
index dfcdc76fdae6d22a6d93599c83c2f7b3f3e73ab4..a42d3f4e9ef67d588b4922b71ac110ce913da40c 100644 (file)
@@ -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);
 
index 6600ee6608574b8829fe0833316f807ac58c3c08..b334d712a1f20f552b6dab689fa57bc26e457c08 100644 (file)
@@ -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 <sstream>
 
-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());
 }
index c02ec8156565110f71ca91732c0eeda8c5db0e04..a49dc3eaeadf6a8e061bc78b36c7917d024c6c99 100644 (file)
@@ -4,11 +4,12 @@
 #include <libguile.h>
 #include <log4cxx/logger.h>
 
+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();
 };