forked from newton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.cpp
342 lines (303 loc) · 8.4 KB
/
terminal.cpp
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#include "terminal.hpp"
#include "td/utils/port/StdStreams.h"
#ifdef USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include "td/utils/find_boundary.h"
namespace td {
void TerminalLogInterface::append(CSlice slice, int log_level) {
auto instance = TerminalIOImpl::instance();
if (!instance) {
default_log_interface->append(slice, log_level);
} else {
instance->deactivate_readline();
std::string color;
if (log_level == 0 || log_level == 1) {
color = TC_RED;
} else if (log_level == 2) {
color = TC_YELLOW;
} else {
color = TC_GREEN;
}
td::TsCerr() << color << slice << TC_EMPTY;
instance->reactivate_readline();
if (log_level == VERBOSITY_NAME(FATAL)) {
process_fatal_error(slice);
}
}
}
void TerminalIOImpl::deactivate_readline() {
out_mutex_.lock();
#ifdef USE_READLINE
if (use_readline_) {
saved_point_ = rl_point;
saved_line_ = std::string(rl_line_buffer, rl_end);
rl_set_prompt("");
rl_replace_line("", 0);
rl_redisplay();
}
#endif
}
void TerminalIOImpl::reactivate_readline() {
#ifdef USE_READLINE
if (use_readline_) {
rl_set_prompt(prompt_.c_str());
rl_point = saved_point_;
rl_replace_line(saved_line_.c_str(), 0);
rl_redisplay();
}
#endif
out_mutex_.unlock();
}
void TerminalIOImpl::output_line(std::string line) {
deactivate_readline();
Stdout().write(line).ensure();
reactivate_readline();
}
void TerminalIOImpl::output_line_stderr(std::string line) {
deactivate_readline();
if (use_readline_) {
Stdout().write(line).ensure();
} else {
Stderr().write(line).ensure();
}
reactivate_readline();
}
void TerminalIOImpl::start_up() {
instance_ = this;
self_ = actor_id(this);
#ifndef USE_READLINE
if (use_readline_) {
use_readline_ = false;
LOG(WARNING) << "disabling readline";
}
#endif
#ifdef USE_READLINE
if (use_readline_) {
deactivate_readline();
rl_getc_function = s_stdin_getc;
rl_callback_handler_install(prompt_.c_str(), s_line);
//rl_attempted_completion_function = tg_cli_completion;
reactivate_readline();
}
#endif
if (!no_input_) {
td::actor::SchedulerContext::get()->get_poll().subscribe(stdin_.get_poll_info().extract_pollable_fd(this),
td::PollFlags::Read());
loop();
}
}
void TerminalIOImpl::tear_down() {
log_interface = default_log_interface;
td::actor::SchedulerContext::get()->get_poll().unsubscribe(stdin_.get_poll_info().get_pollable_fd_ref());
out_mutex_.lock();
#ifdef USE_READLINE
if (use_readline_) {
//out_mutex_.lock();
rl_callback_handler_remove();
//out_mutex_.unlock();
}
#endif
instance_ = nullptr;
out_mutex_.unlock();
log_interface_.release(); // TODO: actually release memory
}
/*void TerminalIOImpl::read_line() {
LOG(DEBUG) << "read line";
while (can_read(stdin_)) {
LOG(DEBUG) << "read line it";
if (buf_end_ == buf_size) {
if (buf_start_ == 0) {
LOG(FATAL) << "too long command";
} else {
std::memmove(buf_, buf_ + buf_start_, buf_end_ - buf_start_);
}
}
auto t = buf_end_;
CHECK(buf_end_ != buf_size);
{
auto R = stdin_.read(td::MutableSlice(buf_ + buf_end_, buf_size - buf_end_)).move_as_ok();
buf_end_ += static_cast<uint32>(R);
}
while (t < buf_end_) {
while (t < buf_end_ && buf_[t] != '\n') {
t++;
}
if (t < buf_end_) {
td::BufferSlice d{t - buf_start_};
d.as_slice().copy_from(td::Slice(buf_ + buf_start_, t - buf_start_));
callback_->line_cb(std::move(d));
t++;
buf_start_ = t;
}
}
}
}*/
void TerminalIOImpl::loop() {
stdin_.flush_read().ignore();
#ifdef USE_READLINE
if (use_readline_) {
while (!stdin_.input_buffer().empty()) {
rl_callback_read_char();
}
} else {
#else
if (1) {
#endif
while (true) {
auto cmd = process_stdin(&stdin_.input_buffer());
if (cmd.is_error()) {
break;
}
cmd_queue_.push(cmd.move_as_ok());
}
}
while (!cmd_queue_.empty()) {
auto cmd = std::move(cmd_queue_.front());
cmd_queue_.pop();
callback_->line_cb(std::move(cmd));
}
}
td::Result<td::BufferSlice> TerminalIOImpl::process_stdin(td::ChainBufferReader *buffer) {
auto found = td::find_boundary(buffer->clone(), "\n", buffer_pos_);
if (!found) {
return Status::Error("End of line not found");
}
auto data = buffer->cut_head(buffer_pos_).move_as_buffer_slice();
if (!data.empty() && data[data.size() - 1] == '\r') {
data.truncate(data.size() - 1);
}
buffer->advance(1);
buffer_pos_ = 0;
return std::move(data);
}
void TerminalIOImpl::s_line(char *line) {
#ifdef USE_READLINE
/* Can use ^D (stty eof) to exit. */
if (line == nullptr) {
LOG(FATAL) << "Closed";
return;
}
CHECK(instance);
if (*line) {
add_history(line);
}
instance()->line_cb(line);
rl_free(line);
#endif
}
int TerminalIOImpl::s_stdin_getc(FILE *) {
return instance()->stdin_getc();
}
void TerminalIOImpl::set_log_interface() {
if (!log_interface_) {
log_interface_ = std::make_unique<TerminalLogInterface>();
}
log_interface = log_interface_.get();
}
int TerminalIOImpl::stdin_getc() {
auto slice = stdin_.input_buffer().prepare_read();
if (slice.empty()) {
return EOF;
}
int res = slice[0];
stdin_.input_buffer().confirm_read(1);
return res;
}
void TerminalIOImpl::line_cb(std::string cmd) {
cmd_queue_.push(td::BufferSlice{std::move(cmd)});
}
void TerminalIO::output_stdout(td::Slice slice, double max_wait) {
auto &fd = td::Stdout();
if (fd.empty()) {
return;
}
double end_time = 0;
while (!slice.empty()) {
auto res = fd.write(slice);
if (res.is_error()) {
if (res.error().code() == EPIPE) {
break;
}
// Resource temporary unavailable
if (end_time == 0) {
end_time = Time::now() + max_wait;
} else if (Time::now() > end_time) {
break;
}
continue;
}
slice.remove_prefix(res.ok());
}
}
void TerminalIO::output(std::string line) {
output(td::Slice(line));
}
void TerminalIO::output(td::Slice line) {
auto instance = TerminalIOImpl::instance();
if (!instance) {
output_stdout(line, 10.0);
} else {
instance->deactivate_readline();
output_stdout(line, 10.0);
instance->reactivate_readline();
}
}
void TerminalIO::output_stderr(std::string line) {
output_stderr(td::Slice(line));
}
void TerminalIO::output_stderr(td::Slice line) {
auto instance = TerminalIOImpl::instance();
if (!instance) {
td::TsCerr() << line;
} else {
if (instance->readline_used()) {
instance->deactivate_readline();
output_stdout(line, 0.01);
instance->reactivate_readline();
} else {
td::TsCerr() << line;
}
}
}
void TerminalIOOutputter::flush() {
if (buffer_) {
CHECK(sb_);
if (!sb_->as_cslice().empty()) {
if (is_err_) {
TerminalIO::output_stderr(sb_->as_cslice());
} else {
TerminalIO::output(sb_->as_cslice());
}
}
sb_->clear();
}
}
TerminalIOOutputter::~TerminalIOOutputter() {
if (buffer_) {
flush();
delete[] buffer_;
}
}
td::actor::ActorOwn<TerminalIO> TerminalIO::create(std::string prompt, bool use_readline, bool no_input,
std::unique_ptr<Callback> callback) {
return actor::create_actor<TerminalIOImpl>(actor::ActorOptions().with_name("terminal io").with_poll(), prompt,
use_readline, no_input, std::move(callback));
}
TerminalIOImpl *TerminalIOImpl::instance_ = nullptr;
} // namespace td