1
0
Fork 0

clean up shell implementation code

This commit is contained in:
Jan Steemann 2015-08-18 18:10:27 +02:00
parent 8677da48b1
commit edd1e90511
11 changed files with 128 additions and 45 deletions

View File

@ -1543,7 +1543,7 @@ static void RunShell (v8::Isolate* isolate, v8::Handle<v8::Context> context, boo
#else
if (BaseClient.colors()) {
if (BaseClient.colors() && console.supportsColors()) {
#ifdef TRI_HAVE_LINENOISE

View File

@ -118,9 +118,9 @@ add_library(
Rest/SslInterface.cpp
Rest/Version.cpp
Statistics/statistics.cpp
Utilities/ScriptLoader.cpp
Utilities/ShellImplementation.cpp
Utilities/DummyShell.cpp
Utilities/LineEditor.cpp
Utilities/ScriptLoader.cpp
Utilities/ShellImplementation.cpp
Utilities/ShellImplFactory.cpp
Zip/ioapi.cpp

View File

@ -91,6 +91,7 @@ lib_libarango_a_SOURCES = \
lib/Rest/SslInterface.cpp \
lib/Rest/Version.cpp \
lib/Statistics/statistics.cpp \
lib/Utilities/DummyShell.cpp \
lib/Utilities/LineEditor.cpp \
lib/Utilities/ScriptLoader.cpp \
lib/Utilities/ShellImplFactory.cpp \
@ -104,10 +105,6 @@ if ENABLE_READLINE
lib_libarango_a_SOURCES += \
lib/Utilities/ReadlineShell.cpp
else
lib_libarango_a_SOURCES += \
lib/Utilities/DummyShell.cpp
endif
################################################################################

View File

@ -107,6 +107,22 @@ namespace arangodb {
////////////////////////////////////////////////////////////////////////////////
std::string getLine (const std::string& prompt, bool& eof) override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not the shell implementation supports colors
////////////////////////////////////////////////////////////////////////////////
bool supportsColors () override final {
return false;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not the shell supports a CTRL-C handler
////////////////////////////////////////////////////////////////////////////////
bool supportsCtrlCHandler () override final {
return false;
}
};
}

View File

@ -107,6 +107,22 @@ namespace arangodb {
////////////////////////////////////////////////////////////////////////////////
std::string getLine (const std::string& prompt, bool& eof) override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not the shell implementation supports colors
////////////////////////////////////////////////////////////////////////////////
bool supportsColors () override final {
return false;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not the shell supports a CTRL-C handler
////////////////////////////////////////////////////////////////////////////////
bool supportsCtrlCHandler () override final {
return false;
}
};
}
#endif

View File

@ -122,34 +122,32 @@ static void ReadlineInputCompleted (char* value) {
auto instance = ReadlineShell::instance();
if (instance == nullptr) {
return;
}
if (instance != nullptr) {
if (instance->getLoopState() == 2) {
if (instance->getLoopState() == 2) {
// CTRL-C received
rl_done = 1;
// CTRL-C received
rl_done = 1;
// replace current input with nothing
rl_replace_line("", 0);
// replace current input with nothing
rl_replace_line("", 0);
if (value != nullptr) {
// avoid memleak
TRI_SystemFree(value);
instance->setLastInput("");
}
else if (value == nullptr) {
rl_done = 1;
rl_replace_line("", 0);
instance->setLoopState(3);
instance->setLastInput("");
}
else {
instance->setLoopState(1);
instance->setLastInput(value == 0 ? "" : value);
}
instance->setLastInput("");
}
else if (value == nullptr) {
rl_done = 1;
rl_replace_line("", 0);
instance->setLoopState(3);
instance->setLastInput("");
}
else {
instance->setLoopState(1);
instance->setLastInput(value == 0 ? "" : value);
if (value != nullptr) {
// avoid memleak
TRI_SystemFree(value);
}
}

View File

@ -125,6 +125,22 @@ namespace arangodb {
void signal () override final;
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not the shell implementation supports colors
////////////////////////////////////////////////////////////////////////////////
bool supportsColors () override final {
return true;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not the shell supports a CTRL-C handler
////////////////////////////////////////////////////////////////////////////////
bool supportsCtrlCHandler () override final {
return true;
}
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------

View File

@ -35,9 +35,8 @@
#include "LinenoiseShell.h"
#elif defined TRI_HAVE_READLINE
#include "ReadlineShell.h"
#else
#include "DummyShell.h"
#endif
#include "DummyShell.h"
using namespace triagens;
using namespace arangodb;
@ -50,12 +49,19 @@ using namespace arangodb;
// --SECTION-- static public methods
// -----------------------------------------------------------------------------
#include <iostream>
////////////////////////////////////////////////////////////////////////////////
/// @brief creates a shell
////////////////////////////////////////////////////////////////////////////////
ShellImplementation* ShellImplFactory::buildShell (std::string const& history,
Completer* completer) {
if (! isatty(STDIN_FILENO)) {
// no keyboard input. use low-level shell without fancy color codes
// and with proper pipe handling
return new DummyShell(history, completer);
}
#ifdef _WIN32
// under Windows the readline is not compilable
return new LinenoiseShell(history, completer);
@ -74,6 +80,10 @@ ShellImplementation* ShellImplFactory::buildShell (std::string const& history,
////////////////////////////////////////////////////////////////////////////////
bool ShellImplFactory::hasCtrlCHandler () {
if (! isatty(STDIN_FILENO)) {
return false;
}
#ifdef _WIN32
// under Windows the readline is not compilable
return false;

View File

@ -102,7 +102,7 @@ namespace arangodb {
// --SECTION-- virtual public methods
// -----------------------------------------------------------------------------
public:
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief handle a signal
@ -149,6 +149,18 @@ namespace arangodb {
virtual std::string getLine (const std::string& prompt, bool& eof) = 0;
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not the shell implementation supports colors
////////////////////////////////////////////////////////////////////////////////
virtual bool supportsColors () = 0;
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not the shell supports a CTRL-C handler
////////////////////////////////////////////////////////////////////////////////
virtual bool supportsCtrlCHandler () = 0;
// -----------------------------------------------------------------------------
// --SECTION-- protected variables
// -----------------------------------------------------------------------------

View File

@ -30,6 +30,7 @@
#include "Basics/logging.h"
#include "Basics/StringUtils.h"
#include "Basics/tri-strings.h"
#include "Utilities/ShellImplementation.h"
#include "Utilities/ShellImplFactory.h"
#include "V8/v8-utils.h"
@ -388,8 +389,6 @@ V8LineEditor::V8LineEditor (v8::Isolate* isolate,
// register global instance
TRI_ASSERT(_instance.load() == nullptr);
_instance.store(this);
setupCtrlCHandler();
}
////////////////////////////////////////////////////////////////////////////////
@ -403,7 +402,23 @@ V8LineEditor::~V8LineEditor () {
}
// -----------------------------------------------------------------------------
// --SECTION-- static protected methods
// --SECTION-- public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not the shell implementation supports colors
////////////////////////////////////////////////////////////////////////////////
bool V8LineEditor::supportsColors () const {
if (_shellImpl == nullptr) {
return false;
}
return _shellImpl->supportsColors();
}
// -----------------------------------------------------------------------------
// --SECTION-- protected methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
@ -412,7 +427,8 @@ V8LineEditor::~V8LineEditor () {
void V8LineEditor::setupCtrlCHandler () {
#ifndef _WIN32
if (ShellImplFactory::hasCtrlCHandler()) {
if (_shellImpl != nullptr &&
_shellImpl->supportsCtrlCHandler()) {
struct sigaction sa;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
@ -433,6 +449,8 @@ void V8LineEditor::setupCtrlCHandler () {
void V8LineEditor::initializeShell () {
_shellImpl = ShellImplFactory::buildShell(_history, &_completer);
setupCtrlCHandler();
}
// -----------------------------------------------------------------------------

View File

@ -161,8 +161,14 @@ namespace arangodb {
return _instance.load();
}
////////////////////////////////////////////////////////////////////////////////
/// @brief whether or not the shell implementation supports colors
////////////////////////////////////////////////////////////////////////////////
bool supportsColors () const;
// -----------------------------------------------------------------------------
// --SECTION-- static protected methods
// --SECTION-- protected methods
// -----------------------------------------------------------------------------
protected:
@ -171,13 +177,7 @@ namespace arangodb {
/// @brief setup a signal handler for CTRL-C
////////////////////////////////////////////////////////////////////////////////
static void setupCtrlCHandler ();
// -----------------------------------------------------------------------------
// --SECTION-- protected methods
// -----------------------------------------------------------------------------
protected:
void setupCtrlCHandler ();
////////////////////////////////////////////////////////////////////////////////
/// @brief creates a concrete Shell with the correct completer