2017-05-29 22:39:49 +00:00
|
|
|
#include "readline_buffer.h"
|
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <unistd.h>
|
2017-06-27 15:10:26 +00:00
|
|
|
#include <boost/thread.hpp>
|
2017-07-07 19:40:32 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2017-05-29 22:39:49 +00:00
|
|
|
|
|
|
|
static void install_line_handler();
|
|
|
|
static void remove_line_handler();
|
|
|
|
|
2017-08-21 11:55:06 +00:00
|
|
|
static boost::mutex sync_mutex;
|
|
|
|
static rdln::linestatus line_stat;
|
|
|
|
static char *the_line;
|
2017-07-07 19:40:32 +00:00
|
|
|
|
2017-05-29 22:39:49 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
rdln::readline_buffer* current = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rdln::suspend_readline::suspend_readline()
|
2017-06-22 12:15:18 +00:00
|
|
|
: m_buffer(NULL), m_restart(false)
|
2017-05-29 22:39:49 +00:00
|
|
|
{
|
|
|
|
m_buffer = current;
|
|
|
|
if(!m_buffer)
|
|
|
|
return;
|
|
|
|
m_restart = m_buffer->is_running();
|
|
|
|
if(m_restart)
|
|
|
|
m_buffer->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
rdln::suspend_readline::~suspend_readline()
|
|
|
|
{
|
|
|
|
if(!m_buffer)
|
|
|
|
return;
|
|
|
|
if(m_restart)
|
|
|
|
m_buffer->start();
|
|
|
|
}
|
|
|
|
|
2017-08-16 13:34:32 +00:00
|
|
|
std::vector<std::string>& rdln::readline_buffer::completion_commands()
|
|
|
|
{
|
|
|
|
static std::vector<std::string> commands = {"exit"};
|
|
|
|
return commands;
|
|
|
|
}
|
|
|
|
|
2017-05-29 22:39:49 +00:00
|
|
|
rdln::readline_buffer::readline_buffer()
|
2017-06-21 16:33:47 +00:00
|
|
|
: std::stringbuf(), m_cout_buf(NULL)
|
2017-05-29 22:39:49 +00:00
|
|
|
{
|
|
|
|
current = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rdln::readline_buffer::start()
|
|
|
|
{
|
|
|
|
if(m_cout_buf != NULL)
|
|
|
|
return;
|
|
|
|
m_cout_buf = std::cout.rdbuf();
|
|
|
|
std::cout.rdbuf(this);
|
|
|
|
install_line_handler();
|
|
|
|
}
|
|
|
|
|
|
|
|
void rdln::readline_buffer::stop()
|
|
|
|
{
|
|
|
|
if(m_cout_buf == NULL)
|
|
|
|
return;
|
|
|
|
std::cout.rdbuf(m_cout_buf);
|
|
|
|
m_cout_buf = NULL;
|
|
|
|
remove_line_handler();
|
|
|
|
}
|
|
|
|
|
2017-08-21 11:55:06 +00:00
|
|
|
rdln::linestatus rdln::readline_buffer::get_line(std::string& line) const
|
2017-05-29 22:39:49 +00:00
|
|
|
{
|
2017-08-21 11:55:06 +00:00
|
|
|
boost::lock_guard<boost::mutex> lock(sync_mutex);
|
|
|
|
line_stat = rdln::partial;
|
|
|
|
rl_callback_read_char();
|
|
|
|
if (line_stat == rdln::full)
|
|
|
|
{
|
|
|
|
line = the_line;
|
|
|
|
free(the_line);
|
|
|
|
the_line = NULL;
|
|
|
|
}
|
|
|
|
return line_stat;
|
2017-05-29 22:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rdln::readline_buffer::set_prompt(const std::string& prompt)
|
|
|
|
{
|
|
|
|
if(m_cout_buf == NULL)
|
|
|
|
return;
|
2017-08-21 11:55:06 +00:00
|
|
|
boost::lock_guard<boost::mutex> lock(sync_mutex);
|
|
|
|
rl_set_prompt(prompt.c_str());
|
2017-05-29 22:39:49 +00:00
|
|
|
rl_redisplay();
|
|
|
|
}
|
|
|
|
|
2017-08-16 13:34:32 +00:00
|
|
|
void rdln::readline_buffer::add_completion(const std::string& command)
|
|
|
|
{
|
|
|
|
if(std::find(completion_commands().begin(), completion_commands().end(), command) != completion_commands().end())
|
|
|
|
return;
|
|
|
|
completion_commands().push_back(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<std::string>& rdln::readline_buffer::get_completions()
|
|
|
|
{
|
|
|
|
return completion_commands();
|
|
|
|
}
|
|
|
|
|
2017-08-21 11:55:06 +00:00
|
|
|
int rdln::readline_buffer::sync()
|
2017-05-29 22:39:49 +00:00
|
|
|
{
|
2017-08-21 11:55:06 +00:00
|
|
|
boost::lock_guard<boost::mutex> lock(sync_mutex);
|
|
|
|
#if RL_READLINE_VERSION < 0x0700
|
|
|
|
char lbuf[2] = {0,0};
|
|
|
|
char *line = NULL;
|
|
|
|
int end = 0, point = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (rl_end || *rl_prompt)
|
2017-06-27 15:10:26 +00:00
|
|
|
{
|
2017-08-21 11:55:06 +00:00
|
|
|
#if RL_READLINE_VERSION >= 0x0700
|
|
|
|
rl_clear_visible_line();
|
|
|
|
#else
|
|
|
|
line = rl_line_buffer;
|
|
|
|
end = rl_end;
|
|
|
|
point = rl_point;
|
|
|
|
rl_line_buffer = lbuf;
|
|
|
|
rl_end = 0;
|
|
|
|
rl_point = 0;
|
|
|
|
rl_save_prompt();
|
|
|
|
rl_redisplay();
|
|
|
|
#endif
|
2017-06-27 15:10:26 +00:00
|
|
|
}
|
2017-05-29 22:39:49 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2017-07-07 19:40:32 +00:00
|
|
|
m_cout_buf->sputc( this->sgetc() );
|
2017-05-29 22:39:49 +00:00
|
|
|
}
|
|
|
|
while ( this->snextc() != EOF );
|
|
|
|
|
2017-08-21 11:55:06 +00:00
|
|
|
#if RL_READLINE_VERSION < 0x0700
|
|
|
|
if (end || *rl_prompt)
|
2017-05-29 22:39:49 +00:00
|
|
|
{
|
2017-08-21 11:55:06 +00:00
|
|
|
rl_restore_prompt();
|
|
|
|
rl_line_buffer = line;
|
|
|
|
rl_end = end;
|
|
|
|
rl_point = point;
|
2017-05-29 22:39:49 +00:00
|
|
|
}
|
2017-08-21 11:55:06 +00:00
|
|
|
#endif
|
2017-07-07 19:40:32 +00:00
|
|
|
rl_on_new_line();
|
2017-05-29 22:39:49 +00:00
|
|
|
rl_redisplay();
|
2017-07-07 19:40:32 +00:00
|
|
|
|
2017-05-29 22:39:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-21 11:55:06 +00:00
|
|
|
static void handle_line(char* line)
|
2017-05-29 22:39:49 +00:00
|
|
|
{
|
2017-08-21 11:55:06 +00:00
|
|
|
bool exit = false;
|
|
|
|
if (line)
|
|
|
|
{
|
|
|
|
line_stat = rdln::full;
|
|
|
|
the_line = line;
|
|
|
|
std::string test_line = line;
|
|
|
|
boost::trim_right(test_line);
|
|
|
|
if(!test_line.empty())
|
|
|
|
{
|
|
|
|
add_history(test_line.c_str());
|
|
|
|
history_set_pos(history_length);
|
|
|
|
if (test_line == "exit" || test_line == "q")
|
|
|
|
exit = true;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
/* EOF */
|
|
|
|
{
|
|
|
|
line_stat = rdln::empty;
|
|
|
|
exit = true;
|
|
|
|
}
|
|
|
|
rl_done = 1;
|
|
|
|
if (exit)
|
|
|
|
rl_set_prompt("");
|
|
|
|
return;
|
2017-05-29 22:39:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-07 19:40:32 +00:00
|
|
|
static char* completion_matches(const char* text, int state)
|
|
|
|
{
|
|
|
|
static size_t list_index;
|
|
|
|
static size_t len;
|
|
|
|
|
|
|
|
if(state == 0)
|
|
|
|
{
|
|
|
|
list_index = 0;
|
|
|
|
len = strlen(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<std::string>& completions = rdln::readline_buffer::get_completions();
|
|
|
|
for(; list_index<completions.size(); )
|
|
|
|
{
|
|
|
|
const std::string& cmd = completions[list_index++];
|
|
|
|
if(cmd.compare(0, len, text) == 0)
|
|
|
|
{
|
|
|
|
return strdup(cmd.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char** attempted_completion(const char* text, int start, int end)
|
|
|
|
{
|
|
|
|
rl_attempted_completion_over = 1;
|
|
|
|
return rl_completion_matches(text, completion_matches);
|
|
|
|
}
|
|
|
|
|
2017-05-29 22:39:49 +00:00
|
|
|
static void install_line_handler()
|
|
|
|
{
|
2017-07-07 19:40:32 +00:00
|
|
|
rl_attempted_completion_function = attempted_completion;
|
2017-05-29 22:39:49 +00:00
|
|
|
rl_callback_handler_install("", handle_line);
|
2017-07-24 13:54:27 +00:00
|
|
|
stifle_history(500);
|
2017-05-29 22:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_line_handler()
|
|
|
|
{
|
2017-07-07 19:40:32 +00:00
|
|
|
rl_replace_line("", 0);
|
|
|
|
rl_set_prompt("");
|
|
|
|
rl_redisplay();
|
2017-05-29 22:39:49 +00:00
|
|
|
rl_callback_handler_remove();
|
|
|
|
}
|
|
|
|
|