diff options
author | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2013-07-24 09:01:28 +0200 |
---|---|---|
committer | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2013-07-24 09:01:28 +0200 |
commit | 316b2a40f0bd9ccdf8a2ff2f2e097011cbb5fedf (patch) | |
tree | ff14daa9091b63c38dabadcf74f128e6aa10d804 | |
parent | 80a6f3a91488c5a45fa0bbc93d91aeb4ebb630c5 (diff) |
Implemented Readline by using libedit.
-rw-r--r-- | includes/BReadline.h | 23 | ||||
-rw-r--r-- | src/Readline.cc | 51 |
2 files changed, 73 insertions, 1 deletions
diff --git a/includes/BReadline.h b/includes/BReadline.h index 6f70f09..73756c4 100644 --- a/includes/BReadline.h +++ b/includes/BReadline.h @@ -1 +1,24 @@ #pragma once + +#include <histedit.h> +#include <BString.h> +#include <Handle.h> + +class Readline { + public: + Readline(const Balau::String & program, Balau::IO<Balau::Handle>); + ~Readline(); + Balau::String gets(); + bool gotEOF() { return m_eof; } + void setPrompt(const Balau::String & prompt); + private: + EditLine * m_el = NULL; + Balau::IO<Balau::Handle> m_in; + bool m_eof = false; + Balau::String m_prompt = "> "; + + static const char * elPrompt(EditLine *); + const char * elPrompt() { return m_prompt.to_charp(); } + static int elGetCFN(EditLine *, char * c); + int elGetCFN(char * c); +}; diff --git a/src/Readline.cc b/src/Readline.cc index 5b47901..a58719d 100644 --- a/src/Readline.cc +++ b/src/Readline.cc @@ -1 +1,50 @@ -#include "BReadline.h"
\ No newline at end of file +#include <stdio.h> +#include "BReadline.h" + +using namespace Balau; + +Readline::Readline(const String & program, IO<Handle> in) + : m_in(in) +{ + m_el = el_init(program.to_charp(), stdin, stdout, stderr); + el_set(m_el, EL_CLIENTDATA, this); + el_set(m_el, EL_PROMPT, (const char * (*)(EditLine *)) &Readline::elPrompt); + el_set(m_el, EL_SIGNAL, 1); + el_set(m_el, EL_GETCFN, (int (*)(EditLine *, char *)) &Readline::elGetCFN); +} + +Readline::~Readline() { + el_end(m_el); +} + +void Readline::setPrompt(const String & prompt) { + m_prompt = prompt; +} + +String Readline::gets() { + int count; + const char * line = el_gets(m_el, &count); + + if (!line) { + m_eof = true; + return ""; + } + + return line; +} + +const char * Readline::elPrompt(EditLine * el) { + Readline * rl; + el_get(el, EL_CLIENTDATA, &rl); + return rl->elPrompt(); +} + +int Readline::elGetCFN(EditLine * el, char * c) { + Readline * rl; + el_get(el, EL_CLIENTDATA, &rl); + return rl->elGetCFN(c); +} + +int Readline::elGetCFN(char * c) { + return m_in->read(c, 1); +} |