aboutsummaryrefslogtreecommitdiff
path: root/contrib/epee/src/readline_buffer.cpp
blob: c5949da0a929650207d22b283f8bf965d8c191fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "readline_buffer.h"
#include <readline/readline.h>
#include <readline/history.h>
#include <iostream>
#include <boost/thread/mutex.hpp>
#include <boost/thread/lock_guard.hpp>
#include <boost/algorithm/string.hpp>

static void install_line_handler();
static void remove_line_handler();

static boost::mutex sync_mutex;
static rdln::linestatus line_stat;
static char *the_line;

namespace
{
  rdln::readline_buffer* current = NULL;
}

rdln::suspend_readline::suspend_readline()
: m_buffer(NULL), m_restart(false)
{
  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();
}

std::vector<std::string>& rdln::readline_buffer::completion_commands()
{
  static std::vector<std::string> commands = {"exit"};
  return commands;
}

rdln::readline_buffer::readline_buffer()
: std::stringbuf(), m_cout_buf(NULL), m_prompt_length(0)
{
  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();
}

rdln::linestatus rdln::readline_buffer::get_line(std::string& line) const
{
  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;
}

void rdln::readline_buffer::set_prompt(const std::string& prompt)
{
  if(m_cout_buf == NULL)
    return;
  boost::lock_guard<boost::mutex> lock(sync_mutex);
  rl_set_prompt(std::string(m_prompt_length, ' ').c_str());
  rl_redisplay();
  rl_set_prompt(prompt.c_str());
  rl_redisplay();
  m_prompt_length = prompt.size();
}

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();
}

int rdln::readline_buffer::sync()
{
  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)
  {
#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
  }

  do
  {
    m_cout_buf->sputc( this->sgetc() );
  }
  while ( this->snextc() != EOF );

#if RL_READLINE_VERSION < 0x0700
  if (end || *rl_prompt)
  {
    rl_restore_prompt();
    rl_line_buffer = line;
    rl_end = end;
    rl_point = point;
  }
#endif
  rl_on_new_line();
  rl_redisplay();

  return 0;
}

static void handle_line(char* line)
{
  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;
}

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);
}

static void install_line_handler()
{
  rl_attempted_completion_function = attempted_completion;
  rl_callback_handler_install("", handle_line);
  stifle_history(500);
}

static void remove_line_handler()
{
  rl_replace_line("", 0);
  rl_set_prompt("");
  rl_redisplay();
  rl_callback_handler_remove();
}