| 1 | #include "readline_buffer.h" |
| 2 | #include <readline/readline.h> |
| 3 | #include <readline/history.h> |
| 4 | #include <iostream> |
| 5 | #include <boost/thread/mutex.hpp> |
| 6 | #include <boost/thread/lock_guard.hpp> |
| 7 | #include <boost/algorithm/string.hpp> |
| 8 | |
| 9 | static bool same_as_last_line(const std::string&); |
| 10 | static void install_line_handler(); |
| 11 | static void remove_line_handler(); |
| 12 | |
| 13 | static boost::mutex sync_mutex; |
| 14 | static rdln::linestatus line_stat; |
| 15 | static char *the_line; |
| 16 | |
| 17 | namespace |
| 18 | { |
| 19 | rdln::readline_buffer* current = NULL; |
| 20 | } |
| 21 | |
| 22 | rdln::suspend_readline::suspend_readline() |
| 23 | : m_buffer(NULL), m_restart(false) |
| 24 | { |
| 25 | m_buffer = current; |
| 26 | if(!m_buffer) |
| 27 | return; |
| 28 | m_restart = m_buffer->is_running(); |
| 29 | if(m_restart) |
| 30 | m_buffer->stop(); |
| 31 | } |
| 32 | |
| 33 | rdln::suspend_readline::~suspend_readline() |
| 34 | { |
| 35 | if(!m_buffer) |
| 36 | return; |
| 37 | if(m_restart) |
| 38 | m_buffer->start(); |
| 39 | } |
| 40 | |
| 41 | std::vector<std::string>& rdln::readline_buffer::completion_commands() |
| 42 | { |
| 43 | static std::vector<std::string> commands = {"exit"}; |
| 44 | return commands; |
| 45 | } |
| 46 | |
| 47 | rdln::readline_buffer::readline_buffer() |
| 48 | : std::stringbuf(), m_cout_buf(NULL), m_prompt_length(0) |
| 49 | { |
| 50 | current = this; |
| 51 | } |
| 52 | |
| 53 | void rdln::readline_buffer::start() |
| 54 | { |
| 55 | boost::lock_guard<boost::mutex> lock(sync_mutex); |
| 56 | if(m_cout_buf != NULL) |
| 57 | return; |
| 58 | m_cout_buf = std::cout.rdbuf(); |
| 59 | std::cout.rdbuf(this); |
| 60 | install_line_handler(); |
| 61 | } |
| 62 | |
| 63 | void rdln::readline_buffer::stop() |
| 64 | { |
| 65 | boost::lock_guard<boost::mutex> lock(sync_mutex); |
| 66 | if(m_cout_buf == NULL) |
| 67 | return; |
| 68 | std::cout.rdbuf(m_cout_buf); |
| 69 | m_cout_buf = NULL; |
| 70 | remove_line_handler(); |
| 71 | } |
| 72 | |
| 73 | rdln::linestatus rdln::readline_buffer::get_line(std::string& line) const |
| 74 | { |
| 75 | boost::lock_guard<boost::mutex> lock(sync_mutex); |
| 76 | line_stat = rdln::partial; |
| 77 | if (!m_cout_buf) |
| 78 | { |
| 79 | line = ""; |
| 80 | return rdln::full; |
| 81 | } |
| 82 | rl_callback_read_char(); |
| 83 | if (line_stat == rdln::full) |
| 84 | { |
| 85 | line = the_line; |
| 86 | free(the_line); |
| 87 | the_line = NULL; |
| 88 | } |
| 89 | return line_stat; |
| 90 | } |
| 91 | |
| 92 | void rdln::readline_buffer::set_prompt(const std::string& prompt) |
| 93 | { |
| 94 | boost::lock_guard<boost::mutex> lock(sync_mutex); |
| 95 | if(m_cout_buf == NULL) |
| 96 | return; |
| 97 | rl_set_prompt(std::string(m_prompt_length, ' ').c_str()); |
| 98 | rl_redisplay(); |
| 99 | rl_set_prompt(prompt.c_str()); |
| 100 | rl_redisplay(); |
| 101 | m_prompt_length = prompt.size(); |
| 102 | } |
| 103 | |
| 104 | void rdln::readline_buffer::add_completion(const std::string& command) |
| 105 | { |
| 106 | if(std::find(completion_commands().begin(), completion_commands().end(), command) != completion_commands().end()) |
| 107 | return; |
| 108 | completion_commands().push_back(command); |
| 109 | } |
| 110 | |
| 111 | const std::vector<std::string>& rdln::readline_buffer::get_completions() |
| 112 | { |
| 113 | return completion_commands(); |
| 114 | } |
| 115 | |
| 116 | int rdln::readline_buffer::sync() |
| 117 | { |
| 118 | boost::lock_guard<boost::mutex> lock(sync_mutex); |
| 119 | |
| 120 | if (m_cout_buf == nullptr) |
| 121 | { |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | #if RL_READLINE_VERSION < 0x0700 |
| 126 | char lbuf[2] = {0,0}; |
| 127 | char *line = NULL; |
| 128 | int end = 0, point = 0; |
| 129 | #endif |
| 130 | |
| 131 | if (rl_end || (rl_prompt && *rl_prompt)) |
| 132 | { |
| 133 | #if RL_READLINE_VERSION >= 0x0700 |
| 134 | rl_clear_visible_line(); |
| 135 | #else |
| 136 | line = rl_line_buffer; |
| 137 | end = rl_end; |
| 138 | point = rl_point; |
| 139 | rl_line_buffer = lbuf; |
| 140 | rl_end = 0; |
| 141 | rl_point = 0; |
| 142 | rl_save_prompt(); |
| 143 | rl_redisplay(); |
| 144 | #endif |
| 145 | } |
| 146 | |
| 147 | do |
| 148 | { |
| 149 | m_cout_buf->sputc( this->sgetc() ); |
| 150 | } |
| 151 | while ( this->snextc() != EOF ); |
| 152 | |
| 153 | #if RL_READLINE_VERSION < 0x0700 |
| 154 | if (end || (rl_prompt && *rl_prompt)) |
| 155 | { |
| 156 | rl_restore_prompt(); |
| 157 | rl_line_buffer = line; |
| 158 | rl_end = end; |
| 159 | rl_point = point; |
| 160 | } |
| 161 | #endif |
| 162 | rl_on_new_line(); |
| 163 | rl_redisplay(); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static void handle_line(char* line) |
| 169 | { |
| 170 | bool exit = false; |
| 171 | if (line) |
| 172 | { |
| 173 | line_stat = rdln::full; |
| 174 | the_line = line; |
| 175 | std::string test_line = line; |
| 176 | boost::trim_right(test_line); |
| 177 | if(!test_line.empty()) |
| 178 | { |
| 179 | if (!same_as_last_line(test_line)) |
| 180 | { |
| 181 | add_history(test_line.c_str()); |
| 182 | history_set_pos(history_length); |
| 183 | } |
| 184 | if (test_line == "exit" || test_line == "q") |
| 185 | exit = true; |
| 186 | } |
| 187 | } else |
| 188 | /* EOF */ |
| 189 | { |
| 190 | line_stat = rdln::empty; |
| 191 | exit = true; |
| 192 | } |
| 193 | rl_done = 1; |
| 194 | if (exit) |
| 195 | rl_set_prompt(""); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | // same_as_last_line returns true, if the last line in the history is |
| 200 | // equal to test_line. |
| 201 | static bool same_as_last_line(const std::string& test_line) |
| 202 | { |
| 203 | // Note that state->offset == state->length, when a new line was entered. |
| 204 | HISTORY_STATE* state = history_get_history_state(); |
| 205 | return state->length > 0 |
| 206 | && test_line.compare(state->entries[state->length-1]->line) == 0; |
| 207 | } |
| 208 | |
| 209 | static char* completion_matches(const char* text, int state) |
| 210 | { |
| 211 | static size_t list_index; |
| 212 | static size_t len; |
| 213 | |
| 214 | if(state == 0) |
| 215 | { |
| 216 | list_index = 0; |
| 217 | len = strlen(text); |
| 218 | } |
| 219 | |
| 220 | const std::vector<std::string>& completions = rdln::readline_buffer::get_completions(); |
| 221 | for(; list_index<completions.size(); ) |
| 222 | { |
| 223 | const std::string& cmd = completions[list_index++]; |
| 224 | if(cmd.compare(0, len, text) == 0) |
| 225 | { |
| 226 | return strdup(cmd.c_str()); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | static char** attempted_completion(const char* text, int start, int end) |
| 234 | { |
| 235 | rl_attempted_completion_over = 1; |
| 236 | return rl_completion_matches(text, completion_matches); |
| 237 | } |
| 238 | |
| 239 | static void install_line_handler() |
| 240 | { |
| 241 | #if RL_READLINE_VERSION >= 0x0801 |
| 242 | rl_variable_bind("enable-bracketed-paste", "off"); |
| 243 | #endif |
| 244 | |
| 245 | rl_attempted_completion_function = attempted_completion; |
| 246 | rl_callback_handler_install("", handle_line); |
| 247 | stifle_history(500); |
| 248 | } |
| 249 | |
| 250 | static void remove_line_handler() |
| 251 | { |
| 252 | rl_replace_line("", 0); |
| 253 | rl_set_prompt(""); |
| 254 | rl_redisplay(); |
| 255 | rl_callback_handler_remove(); |
| 256 | } |
| 257 | |
| 258 | void rdln::clear_screen() |
| 259 | { |
| 260 | rl_clear_screen(0, 0); |
| 261 | } |
| 262 |