diff --git a/Documentation/Books/Users/Transactions/Passing.mdpp b/Documentation/Books/Users/Transactions/Passing.mdpp index c51ac39b67..fea2e42235 100644 --- a/Documentation/Books/Users/Transactions/Passing.mdpp +++ b/Documentation/Books/Users/Transactions/Passing.mdpp @@ -12,7 +12,7 @@ A basic example: params: [ 1, 2, 3 ] }); -The above example will return *1*. +The above example will return *2*. Some example that uses collections: diff --git a/lib/Utilities/DummyShell.cpp b/lib/Utilities/DummyShell.cpp index d8aa1c8f9d..1d9139d88f 100644 --- a/lib/Utilities/DummyShell.cpp +++ b/lib/Utilities/DummyShell.cpp @@ -33,7 +33,7 @@ #include "BasicsC/files.h" using namespace std; -using namespace triagentriagens; +using namespace triagens; // ----------------------------------------------------------------------------- // --SECTION-- class DummyShell @@ -47,10 +47,9 @@ using namespace triagentriagens; /// @brief constructs a new editor //////////////////////////////////////////////////////////////////////////////// -DummyShell::DummyShell (std::string const& history) - : _current(), - _historyFilename(history), - _state(STATE_NONE) { +DummyShell::DummyShell (std::string const& history, + Completer* completer) + : ShellImplementation(history, completer) { } //////////////////////////////////////////////////////////////////////////////// @@ -83,29 +82,6 @@ bool DummyShell::close () { return true; } -//////////////////////////////////////////////////////////////////////////////// -/// @brief get the history file path -//////////////////////////////////////////////////////////////////////////////// - -string DummyShell::historyPath () { - return ""; -} - -//////////////////////////////////////////////////////////////////////////////// -/// @brief add to history -//////////////////////////////////////////////////////////////////////////////// - -void DummyShell::addHistory (char const* str) { -} - -//////////////////////////////////////////////////////////////////////////////// -/// @brief save history -//////////////////////////////////////////////////////////////////////////////// - -bool DummyShell::writeHistory () { - return true; -} - //////////////////////////////////////////////////////////////////////////////// /// @brief line editor prompt //////////////////////////////////////////////////////////////////////////////// @@ -137,7 +113,7 @@ char* DummyShell::prompt (char const* prompt) { p = dotdot.c_str(); if (cin.eof()) { - return 0; + return nullptr; } _current += sep; @@ -164,7 +140,7 @@ char* DummyShell::prompt (char const* prompt) { // extend line and check _current += result; - bool ok = isComplete(_current, lineno, strlen(result)); + bool ok = _completer->isComplete(_current, lineno, strlen(result)); // stop if line is complete if (ok) { @@ -173,11 +149,46 @@ char* DummyShell::prompt (char const* prompt) { } char* line = TRI_DuplicateStringZ(TRI_UNKNOWN_MEM_ZONE, _current.c_str()); + _current.clear(); return line; } +//////////////////////////////////////////////////////////////////////////////// +/// @brief get the history file path +//////////////////////////////////////////////////////////////////////////////// + +string DummyShell::historyPath () { + return ""; +} + +//////////////////////////////////////////////////////////////////////////////// +/// @brief add to history +//////////////////////////////////////////////////////////////////////////////// + +void DummyShell::addHistory (char const* str) { +} + +//////////////////////////////////////////////////////////////////////////////// +/// @brief save history +//////////////////////////////////////////////////////////////////////////////// + +bool DummyShell::writeHistory () { + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +/// @brief returns the characters which the user has typed +/// @arg is the prompt of the shell +/// Note: this is the interface between our shell world and some implementation +/// of key events (linenoise, readline) +//////////////////////////////////////////////////////////////////////////////// + +char* DummyShell::getLine (char const* prompt) { + return this->prompt(prompt); +} + // ----------------------------------------------------------------------------- // --SECTION-- END-OF-FILE // ----------------------------------------------------------------------------- diff --git a/lib/Utilities/DummyShell.h b/lib/Utilities/DummyShell.h index e2b07cb932..91f6b5985e 100644 --- a/lib/Utilities/DummyShell.h +++ b/lib/Utilities/DummyShell.h @@ -39,35 +39,37 @@ #include "V8/v8-utils.h" namespace triagens { - using namespace std; -//////////////////////////////////////////////////////////////////////////////// -/// @addtogroup Shell -/// @{ -//////////////////////////////////////////////////////////////////////////////// + class DummyShell : public ShellImplementation { - class DummyShell: public ShellImplementation { + public: - public: //////////////////////////////////////////////////////////////////////////////// /// public constructor, destructor //////////////////////////////////////////////////////////////////////////////// - DummyShell(string const& history, Completer *); - - virtual ~DummyShell(); + DummyShell (std::string const& history, + Completer*); + + virtual ~DummyShell(); //////////////////////////////////////////////////////////////////////////////// /// @brief line editor open //////////////////////////////////////////////////////////////////////////////// - virtual bool open(bool autoComplete); + virtual bool open (bool autoComplete); //////////////////////////////////////////////////////////////////////////////// /// @brief line editor shutdown //////////////////////////////////////////////////////////////////////////////// - virtual bool close(); + virtual bool close(); + +//////////////////////////////////////////////////////////////////////////////// +/// @brief line editor prompt +//////////////////////////////////////////////////////////////////////////////// + + virtual char* prompt (char const* prompt); //////////////////////////////////////////////////////////////////////////////// /// @brief get the history file path @@ -76,19 +78,19 @@ namespace triagens { /// the local file _historyFilename. //////////////////////////////////////////////////////////////////////////////// - virtual std::string historyPath(); + virtual std::string historyPath(); //////////////////////////////////////////////////////////////////////////////// /// @brief add to history //////////////////////////////////////////////////////////////////////////////// - virtual void addHistory(const char*); + virtual void addHistory (const char*); //////////////////////////////////////////////////////////////////////////////// /// @brief save the history //////////////////////////////////////////////////////////////////////////////// - virtual bool writeHistory(); + virtual bool writeHistory(); //////////////////////////////////////////////////////////////////////////////// /// @brief returns the characters which the user has typed @@ -97,11 +99,7 @@ namespace triagens { /// of key events (linenoise, readline) //////////////////////////////////////////////////////////////////////////////// - virtual char * getLine(char const *); - -//////////////////////////////////////////////////////////////////////////////// -/// @} -//////////////////////////////////////////////////////////////////////////////// + virtual char* getLine (char const*); }; } diff --git a/lib/Utilities/LinenoiseShell.cpp b/lib/Utilities/LinenoiseShell.cpp index a935c1ab02..8eda77d19f 100644 --- a/lib/Utilities/LinenoiseShell.cpp +++ b/lib/Utilities/LinenoiseShell.cpp @@ -157,7 +157,7 @@ bool LinenoiseShell::writeHistory() { return true; } -char * LinenoiseShell::getLine(char const * input) { +char* LinenoiseShell::getLine(char const* input) { return linenoise(input); } diff --git a/lib/Utilities/LinenoiseShell.h b/lib/Utilities/LinenoiseShell.h index 8c8adb0784..4389398d1e 100644 --- a/lib/Utilities/LinenoiseShell.h +++ b/lib/Utilities/LinenoiseShell.h @@ -96,7 +96,7 @@ namespace triagens { /// of key events (linenoise, readline) //////////////////////////////////////////////////////////////////////////////// - virtual char * getLine(char const *); + virtual char* getLine (char const*); //////////////////////////////////////////////////////////////////////////////// /// @} diff --git a/lib/Utilities/ShellImplFactory.cpp b/lib/Utilities/ShellImplFactory.cpp index 3a42294e4f..c9c5525c77 100644 --- a/lib/Utilities/ShellImplFactory.cpp +++ b/lib/Utilities/ShellImplFactory.cpp @@ -28,14 +28,14 @@ /// @author Copyright 2011-2014, triAGENS GmbH, Cologne, Germany //////////////////////////////////////////////////////////////////////////////// - - #ifdef _WIN32 #include "LinenoiseShell.h" #elif TRI_HAVE_LINENOISE #include "LinenoiseShell.h" -#else +#elif TRI_HAVE_READLINE #include "ReadlineShell.h" +#else +#include "DummyShell.h" #endif #include "ShellImplFactory.h" @@ -43,15 +43,19 @@ using namespace triagens; using namespace std; -ShellImplementation * ShellImplFactory::buildShell (string const & history, Completer * completer) { +ShellImplementation* ShellImplFactory::buildShell (string const & history, + Completer* completer) { #ifdef _WIN32 //under windows the readline is not compilable return new LinenoiseShell(history, completer); #elif defined TRI_HAVE_LINENOISE return new LinenoiseShell(history, completer); -#else +#elif defined TRI_HAVE_READLINE return new ReadlineShell(history, completer); +#else + // last resort! + return new DummyShell(history, completer); #endif } diff --git a/lib/Utilities/ShellImplementation.cpp b/lib/Utilities/ShellImplementation.cpp index f8c623fba9..b802e7da28 100644 --- a/lib/Utilities/ShellImplementation.cpp +++ b/lib/Utilities/ShellImplementation.cpp @@ -32,118 +32,123 @@ #include "BasicsC/tri-strings.h" using namespace std; -namespace triagens { - // ----------------------------------------------------------------------------- - // --SECTION-- class ShellImplementation - // ----------------------------------------------------------------------------- - // ----------------------------------------------------------------------------- - // --SECTION-- constructors and destructors - // ----------------------------------------------------------------------------- +namespace triagens { + +// ----------------------------------------------------------------------------- +// --SECTION-- class ShellImplementation +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// --SECTION-- constructors and destructors +// ----------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// /// @brief constructs a new editor //////////////////////////////////////////////////////////////////////////////// - ShellImplementation::ShellImplementation (string const& history, Completer * completer) - : _current(), +ShellImplementation::ShellImplementation (string const& history, + Completer* completer) + : _current(), _historyFilename(history), _state(STATE_NONE), _completer(completer) { - } +} //////////////////////////////////////////////////////////////////////////////// /// @brief destructor //////////////////////////////////////////////////////////////////////////////// - ShellImplementation::~ShellImplementation () { - } +ShellImplementation::~ShellImplementation () { +} - // ----------------------------------------------------------------------------- - // --SECTION-- public methods - // ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- +// --SECTION-- public methods +// ----------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// /// @brief line editor prompt //////////////////////////////////////////////////////////////////////////////// - char* ShellImplementation::prompt (char const* the_prompt) { - string dotdot; - char const* p = the_prompt; - size_t len1 = strlen(the_prompt); - size_t len2 = len1; - size_t lineno = 0; +char* ShellImplementation::prompt (char const* the_prompt) { + string dotdot; + char const* p = the_prompt; + size_t len1 = strlen(the_prompt); + size_t len2 = len1; + size_t lineno = 0; - if (len1 < 3) { - dotdot = "> "; - len2 = 2; - } - else { - dotdot = string(len1 - 2, '.') + "> "; - } - - char const* sep = ""; - - while (true) { - // calling concrete implmentation of the shell - char* result = getLine(p); - - p = dotdot.c_str(); - - if (result == 0) { - - // give up, if the user pressed control-D on the top-most level - if (_current.empty()) { - return 0; - } - - // otherwise clear current content - _current.clear(); - break; - } - - _current += sep; - sep = "\n"; - ++lineno; - - // remove any the_prompt at the beginning of the line - char* originalLine = result; - bool c1 = strncmp(result, the_prompt, len1) == 0; - bool c2 = strncmp(result, dotdot.c_str(), len2) == 0; - - while (c1 || c2) { - if (c1) { - result += len1; - } - else if (c2) { - result += len2; - } - - c1 = strncmp(result, the_prompt, len1) == 0; - c2 = strncmp(result, dotdot.c_str(), len2) == 0; - } - - // extend line and check - _current += result; - - bool ok = _completer->isComplete(_current, lineno, strlen(result)); - - // cannot use TRI_Free, because it was allocated by the system call readline - TRI_SystemFree(originalLine); - - // stop if line is complete - if (ok) { - break; - } - } - - char* line = TRI_DuplicateStringZ(TRI_UNKNOWN_MEM_ZONE, _current.c_str()); - _current.clear(); - - return line; + if (len1 < 3) { + dotdot = "> "; + len2 = 2; } + else { + dotdot = string(len1 - 2, '.') + "> "; + } + + char const* sep = ""; + + while (true) { + // calling concrete implmentation of the shell + char* result = getLine(p); + + p = dotdot.c_str(); + + if (result == nullptr) { + + // give up, if the user pressed control-D on the top-most level + if (_current.empty()) { + return nullptr; + } + + // otherwise clear current content + _current.clear(); + break; + } + + _current += sep; + sep = "\n"; + ++lineno; + + // remove any the_prompt at the beginning of the line + char* originalLine = result; + bool c1 = strncmp(result, the_prompt, len1) == 0; + bool c2 = strncmp(result, dotdot.c_str(), len2) == 0; + + while (c1 || c2) { + if (c1) { + result += len1; + } + else if (c2) { + result += len2; + } + + c1 = strncmp(result, the_prompt, len1) == 0; + c2 = strncmp(result, dotdot.c_str(), len2) == 0; + } + + // extend line and check + _current += result; + + bool ok = _completer->isComplete(_current, lineno, strlen(result)); + + // cannot use TRI_Free, because it was allocated by the system call readline + TRI_SystemFree(originalLine); + + // stop if line is complete + if (ok) { + break; + } + } + + char* line = TRI_DuplicateStringZ(TRI_UNKNOWN_MEM_ZONE, _current.c_str()); + _current.clear(); + + return line; } + +} + // ----------------------------------------------------------------------------- // --SECTION-- END-OF-FILE // ----------------------------------------------------------------------------- diff --git a/lib/Utilities/ShellImplementation.h b/lib/Utilities/ShellImplementation.h index 9e5dfd0686..e1b1778cce 100644 --- a/lib/Utilities/ShellImplementation.h +++ b/lib/Utilities/ShellImplementation.h @@ -37,10 +37,13 @@ namespace triagens { class ShellImplementation { + //////////////////////////////////////////////////////////////////////////////// /// @brief state of the console protected types //////////////////////////////////////////////////////////////////////////////// + protected: + typedef enum { STATE_NONE = 0, STATE_OPENED, @@ -49,11 +52,12 @@ namespace triagens { console_state_e; public: + //////////////////////////////////////////////////////////////////////////////// /// public constructor, destructor //////////////////////////////////////////////////////////////////////////////// - ShellImplementation (std::string const& history, Completer *); + ShellImplementation (std::string const& history, Completer*); virtual ~ShellImplementation (); @@ -61,7 +65,7 @@ namespace triagens { /// @brief line editor open //////////////////////////////////////////////////////////////////////////////// - virtual bool open (bool autoComplete) = 0; + virtual bool open (bool autoComplete) = 0; //////////////////////////////////////////////////////////////////////////////// /// @brief line editor shutdown @@ -100,15 +104,11 @@ namespace triagens { /// @brief todo!! //////////////////////////////////////////////////////////////////////////////// - virtual char * getLine (const char*) = 0; + virtual char* getLine (const char*) = 0; -//////////////////////////////////////////////////////////////////////////////// -/// @} -//////////////////////////////////////////////////////////////////////////////// - - // ----------------------------------------------------------------------------- - // --SECTION-- protected variables - // ----------------------------------------------------------------------------- +// ----------------------------------------------------------------------------- +// --SECTION-- protected variables +// ----------------------------------------------------------------------------- protected: @@ -134,11 +134,13 @@ namespace triagens { /// @brief object which defines when the input is finished //////////////////////////////////////////////////////////////////////////////// - Completer * _completer; + Completer* _completer; }; } + #endif + // ----------------------------------------------------------------------------- // --SECTION-- END-OF-FILE // -----------------------------------------------------------------------------