From dba058b3ff87f818042a09bcfa8fc58072adc49b Mon Sep 17 00:00:00 2001 From: Christoph Egger Date: Thu, 27 Nov 2014 13:16:05 +0100 Subject: [PATCH] Properly handle errors in sceme Catch exceptions, no longer exit on them and print the content on the console --- src/gui/widgets/ScriptingDock.cxx | 25 +++++++++++++++++++++++-- src/gui/widgets/ScriptingDock.hxx | 5 ++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/gui/widgets/ScriptingDock.cxx b/src/gui/widgets/ScriptingDock.cxx index b354570..6a3f763 100644 --- a/src/gui/widgets/ScriptingDock.cxx +++ b/src/gui/widgets/ScriptingDock.cxx @@ -1,15 +1,36 @@ #include "ScriptingDock.hxx" +namespace { + SCM handler (void *data, SCM tag, SCM throw_args) { + scm_handle_by_message_noexit ((void*)"foo", tag, throw_args); + return SCM_BOOL_F; + } +} + void ScriptingDock::doEvaluate() { QString text = line->text(); line->clear(); LOG4CXX_INFO(logger, "Evaluating String \"" << text.toStdString() << "\""); browser->append(QString("> ") + text); - SCM result_obj = scm_c_eval_string(text.toStdString().c_str()); + 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); - browser->append(scm_to_locale_string(output)); + QString output_q = scm_to_locale_string(output); + if (output_q.endsWith("\n")) output_q.chop(1); + if (output_q != "") browser->append(output_q); + + 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); + + scm_truncate_file(guile_output_port, scm_from_uint16(0)); + scm_truncate_file(guile_error_port, scm_from_uint16(0)); + browser->append(scm_to_locale_string(result_str)); } diff --git a/src/gui/widgets/ScriptingDock.hxx b/src/gui/widgets/ScriptingDock.hxx index d60158b..e070955 100644 --- a/src/gui/widgets/ScriptingDock.hxx +++ b/src/gui/widgets/ScriptingDock.hxx @@ -21,17 +21,20 @@ public: connect(line, SIGNAL(returnPressed()), this, SLOT(doEvaluate())); scm_init_guile(); 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); } private: log4cxx::LoggerPtr logger; - + QTextBrowser * browser; QGridLayout * layout; QPushButton * button; QLineEdit * line; SCM guile_output_port; + SCM guile_error_port; private slots: void doEvaluate(); }; -- 2.39.5