mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
initial commit
This commit is contained in:
commit
c2da007f40
1610 changed files with 398047 additions and 0 deletions
49
terminal/CMakeLists.txt
Normal file
49
terminal/CMakeLists.txt
Normal file
|
@ -0,0 +1,49 @@
|
|||
cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
|
||||
|
||||
if (NOT OPENSSL_FOUND)
|
||||
find_package(OpenSSL REQUIRED)
|
||||
endif()
|
||||
|
||||
set(TERMINAL_SOURCE
|
||||
terminal.cpp
|
||||
|
||||
terminal.h
|
||||
terminal.hpp
|
||||
)
|
||||
|
||||
add_library(terminal STATIC ${TERMINAL_SOURCE})
|
||||
target_include_directories(terminal PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/..
|
||||
${OPENSSL_INCLUDE_DIR}
|
||||
)
|
||||
target_link_libraries(terminal PRIVATE tdutils tdactor )
|
||||
|
||||
if (NOT READLINE_FOUND)
|
||||
find_package(Readline)
|
||||
endif()
|
||||
if (NOT READLINE_FOUND)
|
||||
message(STATUS "Could NOT find Readline (this is NOT an error)")
|
||||
else()
|
||||
message(STATUS "Found Readline: ${READLINE_INCLUDE_DIR} ${READLINE_LIBRARY}")
|
||||
if (NOT USABLE_READLINE_FOUND)
|
||||
set(CMAKE_REQUIRED_INCLUDES "${READLINE_INCLUDE_DIR}")
|
||||
set(CMAKE_REQUIRED_LIBRARIES "${READLINE_LIBRARY}")
|
||||
include(CheckCXXSourceCompiles)
|
||||
unset(USABLE_READLINE_FOUND CACHE)
|
||||
check_cxx_source_compiles("#include <stdio.h>\n#include <readline/readline.h>\nint main() { rl_free(0); }" USABLE_READLINE_FOUND)
|
||||
if (NOT USABLE_READLINE_FOUND)
|
||||
message(STATUS "Found Readline is too old, ignore it (this is NOT an error)")
|
||||
unset(READLINE_INCLUDE_DIR CACHE)
|
||||
unset(READLINE_LIBRARY CACHE)
|
||||
endif()
|
||||
endif()
|
||||
if (USABLE_READLINE_FOUND)
|
||||
target_link_libraries(terminal PRIVATE ${READLINE_LIBRARY})
|
||||
target_include_directories(terminal SYSTEM PRIVATE ${READLINE_INCLUDE_DIR})
|
||||
target_compile_definitions(terminal PRIVATE -DUSE_READLINE=1)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
|
||||
|
272
terminal/terminal.cpp
Normal file
272
terminal/terminal.cpp
Normal file
|
@ -0,0 +1,272 @@
|
|||
/*
|
||||
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-2019 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();
|
||||
td::TsCerr() << TC_GREEN << 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::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
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
/*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(std::string line) {
|
||||
auto instance_ = TerminalIOImpl::instance();
|
||||
if (!instance_) {
|
||||
std::cout << line;
|
||||
} else {
|
||||
instance_->deactivate_readline();
|
||||
td::TsCerr() << line;
|
||||
instance_->reactivate_readline();
|
||||
}
|
||||
}
|
||||
|
||||
void TerminalIO::output(td::Slice line) {
|
||||
auto instance_ = TerminalIOImpl::instance();
|
||||
if (!instance_) {
|
||||
td::TsCerr() << line;
|
||||
} else {
|
||||
instance_->deactivate_readline();
|
||||
td::TsCerr() << line;
|
||||
instance_->reactivate_readline();
|
||||
}
|
||||
}
|
||||
|
||||
TerminalIOOutputter::~TerminalIOOutputter() {
|
||||
if (buffer_) {
|
||||
CHECK(sb_);
|
||||
TerminalIO::output(sb_->as_cslice());
|
||||
delete[] buffer_;
|
||||
}
|
||||
}
|
||||
|
||||
td::actor::ActorOwn<TerminalIO> TerminalIO::create(std::string prompt, bool use_readline,
|
||||
std::unique_ptr<Callback> callback) {
|
||||
return actor::create_actor<TerminalIOImpl>(actor::ActorOptions().with_name("terminal io").with_poll(), prompt,
|
||||
use_readline, std::move(callback));
|
||||
}
|
||||
|
||||
TerminalIOImpl *TerminalIOImpl::instance_ = nullptr;
|
||||
|
||||
} // namespace td
|
88
terminal/terminal.h
Normal file
88
terminal/terminal.h
Normal file
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
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-2019 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "td/actor/actor.h"
|
||||
#include "td/utils/buffer.h"
|
||||
|
||||
#include <functional>
|
||||
#include <ostream>
|
||||
|
||||
namespace td {
|
||||
|
||||
class TerminalIOOutputter {
|
||||
public:
|
||||
static const size_t BUFFER_SIZE = 128 * 1024;
|
||||
TerminalIOOutputter()
|
||||
: buffer_(new char[BUFFER_SIZE]), sb_(std::make_unique<StringBuilder>(td::MutableSlice{buffer_, BUFFER_SIZE})) {
|
||||
}
|
||||
TerminalIOOutputter(TerminalIOOutputter &&X) = default;
|
||||
|
||||
template <class T>
|
||||
TerminalIOOutputter &operator<<(const T &other) {
|
||||
*sb_ << other;
|
||||
return *this;
|
||||
}
|
||||
TerminalIOOutputter &operator<<(std::ostream &(*pManip)(std::ostream &)) {
|
||||
*sb_ << '\n';
|
||||
return *this;
|
||||
}
|
||||
|
||||
auto &sb() {
|
||||
return *sb_;
|
||||
}
|
||||
|
||||
MutableCSlice as_cslice() {
|
||||
return sb_->as_cslice();
|
||||
}
|
||||
bool is_error() const {
|
||||
return sb_->is_error();
|
||||
}
|
||||
~TerminalIOOutputter();
|
||||
|
||||
private:
|
||||
char *buffer_;
|
||||
std::unique_ptr<StringBuilder> sb_;
|
||||
};
|
||||
|
||||
class TerminalIO : public actor::Actor {
|
||||
public:
|
||||
class Callback {
|
||||
public:
|
||||
virtual ~Callback() = default;
|
||||
virtual void line_cb(td::BufferSlice line) = 0;
|
||||
//virtual std::vector<std::string> autocomplete_cb(std::string line) = 0;
|
||||
};
|
||||
|
||||
virtual ~TerminalIO() = default;
|
||||
virtual void update_prompt(std::string new_prompt) = 0;
|
||||
virtual void update_callback(std::unique_ptr<Callback> callback) = 0;
|
||||
static void output(std::string line);
|
||||
static void output(td::Slice slice);
|
||||
static TerminalIOOutputter out() {
|
||||
return TerminalIOOutputter{};
|
||||
}
|
||||
virtual void output_line(std::string line) = 0;
|
||||
virtual void set_log_interface() = 0;
|
||||
|
||||
static td::actor::ActorOwn<TerminalIO> create(std::string prompt, bool use_readline,
|
||||
std::unique_ptr<Callback> callback);
|
||||
};
|
||||
|
||||
} // namespace td
|
100
terminal/terminal.hpp
Normal file
100
terminal/terminal.hpp
Normal file
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
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-2019 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "terminal.h"
|
||||
#include <iostream>
|
||||
#include "td/utils/port/FileFd.h"
|
||||
#include "td/utils/port/StdStreams.h"
|
||||
#include <queue>
|
||||
|
||||
namespace td {
|
||||
|
||||
class TerminalLogInterface : public LogInterface {
|
||||
public:
|
||||
void append(CSlice slice, int log_level) override;
|
||||
};
|
||||
|
||||
class TerminalIOImpl : public TerminalIO, td::ObserverBase {
|
||||
public:
|
||||
void refresh() {
|
||||
}
|
||||
void update_prompt(std::string new_prompt) override {
|
||||
prompt_ = new_prompt;
|
||||
refresh();
|
||||
};
|
||||
void update_callback(std::unique_ptr<Callback> callback) override {
|
||||
callback_ = std::move(callback);
|
||||
}
|
||||
void deactivate_readline();
|
||||
void reactivate_readline();
|
||||
void output_line(std::string line) override;
|
||||
void set_log_interface() override;
|
||||
//void read_line();
|
||||
void loop() override;
|
||||
void start_up() override;
|
||||
void tear_down() override;
|
||||
|
||||
static TerminalIOImpl *instance() {
|
||||
return instance_;
|
||||
}
|
||||
|
||||
void notify() override {
|
||||
// NB: Interface will be changed
|
||||
td::actor::send_closure_later(self_, &TerminalIOImpl::on_net);
|
||||
}
|
||||
void on_net() {
|
||||
loop();
|
||||
}
|
||||
TerminalIOImpl(std::string prompt, bool use_readline, std::unique_ptr<Callback> callback)
|
||||
: prompt_(prompt), use_readline_(use_readline), callback_(std::move(callback)) {
|
||||
}
|
||||
|
||||
int stdin_getc();
|
||||
void line_cb(std::string line);
|
||||
static int s_stdin_getc(FILE *);
|
||||
static void s_line(char *line);
|
||||
|
||||
td::Result<td::BufferSlice> process_stdin(td::ChainBufferReader *buffer);
|
||||
|
||||
private:
|
||||
static constexpr td::uint32 buf_size = 1 << 20;
|
||||
td::BufferedStdin stdin_;
|
||||
|
||||
std::string prompt_;
|
||||
bool use_readline_ = false;
|
||||
std::unique_ptr<Callback> callback_;
|
||||
std::mutex out_mutex_;
|
||||
|
||||
char buf_[buf_size];
|
||||
td::uint32 buf_start_ = 0;
|
||||
td::uint32 buf_end_ = 0;
|
||||
|
||||
static TerminalIOImpl *instance_;
|
||||
td::actor::ActorId<TerminalIOImpl> self_;
|
||||
std::unique_ptr<TerminalLogInterface> log_interface_;
|
||||
|
||||
td::uint32 saved_point_;
|
||||
std::string saved_line_;
|
||||
size_t buffer_pos_ = 0;
|
||||
|
||||
std::queue<td::BufferSlice> cmd_queue_;
|
||||
};
|
||||
|
||||
} // namespace td
|
Loading…
Add table
Add a link
Reference in a new issue