Implement unicode on console, move readline into driver
This commit is contained in:
@@ -69,108 +69,22 @@ static DrivenEngine *make_engine(std::string_view kind) {
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
Channel::Channel(DrivenEngine *de, int chid, int port, const eng::string &target, bool stop) {
|
||||
chid_ = chid;
|
||||
port_ = port;
|
||||
closed_ = false;
|
||||
target_ = target;
|
||||
readline_lastc_ = 0;
|
||||
desired_prompt_ = "";
|
||||
stop_driver_ = stop;
|
||||
sb_in_ = eng::make_shared<StreamBuffer>();
|
||||
sb_out_ = eng::make_shared<StreamBuffer>();
|
||||
sb_drvout_ = sb_out_;
|
||||
}
|
||||
|
||||
void Channel::erase_command() {
|
||||
int ccsize = current_prompt_.size() + current_command_.size();
|
||||
if (ccsize > 0) {
|
||||
sb_drvout_->write_bytes(util::repeat_string("\b \b", ccsize));
|
||||
current_prompt_ = "";
|
||||
current_command_ = "";
|
||||
}
|
||||
}
|
||||
|
||||
void Channel::echo_command() {
|
||||
// If the prompt has changed, erase everything and start over.
|
||||
if (desired_prompt_ != current_prompt_) {
|
||||
int ccsize = current_prompt_.size() + current_command_.size();
|
||||
sb_drvout_->write_bytes(util::repeat_string("\b \b", ccsize));
|
||||
sb_drvout_->write_bytes(desired_prompt_);
|
||||
current_command_ = "";
|
||||
current_prompt_ = desired_prompt_;
|
||||
}
|
||||
|
||||
// Find out how much of the command matches.
|
||||
int match = sv::common_prefix_length(current_command_, desired_command_);
|
||||
|
||||
// Echo backspaces to remove the non-matching part.
|
||||
int remove = current_command_.size() - match;
|
||||
if (remove > 0) {
|
||||
sb_drvout_->write_bytes(util::repeat_string("\b \b", remove));
|
||||
current_command_ = current_command_.substr(0, match);
|
||||
}
|
||||
|
||||
// Echo the new part.
|
||||
eng::string newpart = desired_command_.substr(current_command_.size());
|
||||
if (newpart != "") {
|
||||
sb_drvout_->write_bytes(newpart);
|
||||
current_command_ = desired_command_;
|
||||
}
|
||||
}
|
||||
|
||||
void Channel::set_prompt(const eng::string &p) {
|
||||
desired_prompt_ = p;
|
||||
}
|
||||
|
||||
void Channel::feed_readline(std::string_view data) {
|
||||
int nbytes = data.size();
|
||||
const char *bytes = data.data();
|
||||
for (int i = 0; i < nbytes; i++) {
|
||||
char c = bytes[i];
|
||||
if ((c == '\n') && (readline_lastc_ == '\r')) {
|
||||
// Ignore newline immediately after carriage return.
|
||||
// Otherwise, crlf produces two newlines.
|
||||
} else if ((c == '\r') || (c == '\n')) {
|
||||
echo_command();
|
||||
sb_drvout_->write_bytes(eng::string(" \n"));
|
||||
sb_in_->write_bytes(desired_command_);
|
||||
sb_in_->write_uint8('\n');
|
||||
desired_command_ = "";
|
||||
current_prompt_ = "";
|
||||
current_command_ = "";
|
||||
} else if ((c == '\b') || (c == 127)) {
|
||||
int len = desired_command_.size();
|
||||
if (len > 0) {
|
||||
desired_command_ = desired_command_.substr(0, len-1);
|
||||
}
|
||||
} else if (c >= 32) {
|
||||
int len = desired_command_.size();
|
||||
if (len < READLINE_MAX) {
|
||||
desired_command_ = desired_command_ + c;
|
||||
}
|
||||
}
|
||||
readline_lastc_ = c;
|
||||
}
|
||||
}
|
||||
|
||||
std::string_view Channel::peek_outgoing() const {
|
||||
return sb_drvout_->view();
|
||||
}
|
||||
|
||||
void Channel::pump_readline() {
|
||||
if (sb_drvout_ != sb_out_) {
|
||||
if (!sb_out_->empty()) {
|
||||
erase_command();
|
||||
sb_out_->transfer_into(sb_drvout_.get());
|
||||
}
|
||||
echo_command();
|
||||
}
|
||||
return sb_out_->view();
|
||||
}
|
||||
|
||||
void Channel::sent_outgoing(int nbytes) {
|
||||
sb_drvout_->read_bytes(nbytes);
|
||||
sb_out_->read_bytes(nbytes);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
@@ -232,7 +146,6 @@ void DrivenEngine::stop_driver() {
|
||||
DrivenEngine::DrivenEngine() {
|
||||
next_unused_chid_ = 1;
|
||||
stdio_channel_ = eng::make_shared<Channel>(this, 0, 0, "", false);
|
||||
stdio_channel_->sb_drvout_ = eng::make_shared<StreamBuffer>();
|
||||
channels_[0] = stdio_channel_;
|
||||
rescan_lua_source_ = false;
|
||||
clock_ = 0.0;
|
||||
@@ -492,7 +405,6 @@ bool DrivenEngine::drv_get_stop_driver() const {
|
||||
void DrivenEngine::drv_initialize(uint32_t srcpklen, const char *srcpk, int argc, char **argv) {
|
||||
drv_set_lua_source(srcpklen, srcpk);
|
||||
event_init(argc, argv);
|
||||
stdio_channel_->pump_readline();
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_clear_new_outgoing() {
|
||||
@@ -504,14 +416,10 @@ void DrivenEngine::drv_sent_outgoing(uint32_t chid, uint32_t nbytes) {
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_recv_incoming(uint32_t chid, uint32_t nbytes, const char *bytes) {
|
||||
std::string_view sbytes(bytes, nbytes);
|
||||
if (nbytes > 0) {
|
||||
std::string_view sbytes(bytes, nbytes);
|
||||
Channel *ch = get_chid(chid);
|
||||
if (ch->sb_drvout_ != ch->sb_out_) {
|
||||
ch->feed_readline(sbytes);
|
||||
} else {
|
||||
ch->sb_in_->write_bytes(sbytes);
|
||||
}
|
||||
ch->sb_in_->write_bytes(sbytes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -532,7 +440,6 @@ uint32_t DrivenEngine::drv_notify_accept(uint32_t port) {
|
||||
void DrivenEngine::drv_invoke_event_update(double clock) {
|
||||
clock_ = clock;
|
||||
event_update();
|
||||
stdio_channel_->pump_readline();
|
||||
}
|
||||
|
||||
void DrivenEngine::drv_set_lua_source(uint32_t srcpklen, const char *srcpk) {
|
||||
|
||||
@@ -88,10 +88,6 @@ public:
|
||||
//
|
||||
const eng::string &error() const { return error_; }
|
||||
|
||||
// Set the prompt for readline mode.
|
||||
//
|
||||
void set_prompt(const eng::string &prompt);
|
||||
|
||||
// Do not construct your own Channels. Instead,
|
||||
// use methods of class DrivenEngine like new_outgoing_channel.
|
||||
// Channels are referenced by shared_ptr. You can
|
||||
@@ -104,40 +100,22 @@ private:
|
||||
// Constructor is deliberately private. Use
|
||||
// DrivenEngine::new_outgoing_channel to create outgoing socket channels.
|
||||
//
|
||||
|
||||
void feed_readline(std::string_view data);
|
||||
std::string_view peek_outgoing() const;
|
||||
void sent_outgoing(int nbytes);
|
||||
void erase_command();
|
||||
void echo_command();
|
||||
void pump_readline();
|
||||
|
||||
private:
|
||||
static const int READLINE_MAX=512;
|
||||
int chid_;
|
||||
|
||||
// These are the in/out buffers presented to the user.
|
||||
std::shared_ptr<StreamBuffer> sb_in_;
|
||||
std::shared_ptr<StreamBuffer> sb_out_;
|
||||
|
||||
// If this is stdio, we inject tty echoes into the output stream.
|
||||
// This buffer holds the users output interleaved with the tty echoes.
|
||||
// In any other channel, this is just another pointer to sb_out.
|
||||
std::shared_ptr<StreamBuffer> sb_drvout_;
|
||||
|
||||
int port_;
|
||||
bool closed_;
|
||||
eng::string error_;
|
||||
eng::string target_;
|
||||
bool stop_driver_;
|
||||
|
||||
// Readline stuff. Only used on channel 0 (stdio).
|
||||
eng::string desired_command_;
|
||||
eng::string current_command_;
|
||||
eng::string desired_prompt_;
|
||||
eng::string current_prompt_;
|
||||
char readline_lastc_;
|
||||
|
||||
friend class DrivenEngine;
|
||||
};
|
||||
|
||||
@@ -215,9 +193,10 @@ public:
|
||||
// Obtain the stdio channel. There is only one stdio channel.
|
||||
//
|
||||
// DRIVER: the stdio channel is created automatically when the DrivenEngine
|
||||
// is created. The driver is responsible for relaying data into the channel
|
||||
// using drv_get_target, drv_peek_outgoing, drv_sent_outgoing,
|
||||
// drv_recv_incoming.
|
||||
// is created. Stdio should be connected to a console which is in
|
||||
// line-at-a-time mode. The driver is responsible for relaying data from
|
||||
// the console into the stdio channel using drv_peek_outgoing,
|
||||
// drv_sent_outgoing, drv_recv_incoming.
|
||||
//
|
||||
SharedChannel get_stdio_channel();
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ public:
|
||||
channel_ = new_outgoing_channel("nocert:localhost:8085");
|
||||
|
||||
// Set the console prompt
|
||||
get_stdio_channel()->set_prompt(console_.get_prompt());
|
||||
// get_stdio_channel()->set_prompt(console_.get_prompt());
|
||||
|
||||
// The driver loads the lua source automatically.
|
||||
// However, we don't need it. Throw it out.
|
||||
@@ -252,7 +252,7 @@ public:
|
||||
eng::string line = get_stdio_channel()->in()->readline();
|
||||
if (line == "") break;
|
||||
console_.add(line);
|
||||
get_stdio_channel()->set_prompt(console_.get_prompt());
|
||||
// get_stdio_channel()->set_prompt(console_.get_prompt());
|
||||
do_command(console_.get_command());
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ public:
|
||||
listen_port(8080);
|
||||
|
||||
// Set the console prompt.
|
||||
get_stdio_channel()->set_prompt(console_.get_prompt());
|
||||
// get_stdio_channel()->set_prompt(console_.get_prompt());
|
||||
}
|
||||
|
||||
void do_luainvoke_command(const util::StringVec &words) {
|
||||
@@ -172,7 +172,7 @@ public:
|
||||
eng::string line = get_stdio_channel()->in()->readline();
|
||||
if (line == "") break;
|
||||
console_.add(line);
|
||||
get_stdio_channel()->set_prompt(console_.get_prompt());
|
||||
// get_stdio_channel()->set_prompt(console_.get_prompt());
|
||||
do_command(console_.get_command());
|
||||
}
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ private:
|
||||
world_->run_unittests();
|
||||
actor_id_ = world_->create_login_actor();
|
||||
stdostream() << "Login actor ID: " << actor_id_ << std::endl;
|
||||
get_stdio_channel()->set_prompt(console_.get_prompt());
|
||||
//get_stdio_channel()->set_prompt(console_.get_prompt());
|
||||
}
|
||||
|
||||
void event_update()
|
||||
@@ -113,7 +113,7 @@ private:
|
||||
eng::string line = get_stdio_channel()->in()->readline();
|
||||
if (line == "") break;
|
||||
console_.add(line);
|
||||
get_stdio_channel()->set_prompt(console_.get_prompt());
|
||||
//get_stdio_channel()->set_prompt(console_.get_prompt());
|
||||
do_command(console_.get_command());
|
||||
if (print_channeler_.channel(world_->get_printbuffer(actor_id_), stdostream())) {
|
||||
world_->invoke(print_channeler_.invocation(actor_id_));
|
||||
|
||||
@@ -301,6 +301,7 @@ int32_t read_ascii_char(string_view &source) {
|
||||
int32_t read_codepoint_utf8(string_view &source) {
|
||||
size_t size = source.size();
|
||||
if (size == 0) return -1;
|
||||
|
||||
const unsigned char *bytes = (const unsigned char *)source.data();
|
||||
int codepoint;
|
||||
size_t seqlen;
|
||||
@@ -321,7 +322,9 @@ int32_t read_codepoint_utf8(string_view &source) {
|
||||
codepoint = (bytes[0] & 0x07);
|
||||
seqlen = 4;
|
||||
} else {
|
||||
return -1;
|
||||
// Bad character. Drop a byte and return invalid CP.
|
||||
source.remove_prefix(1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (seqlen > size) {
|
||||
@@ -329,7 +332,11 @@ int32_t read_codepoint_utf8(string_view &source) {
|
||||
}
|
||||
|
||||
for (size_t i = 1; i < seqlen; ++i) {
|
||||
if ((bytes[i] & 0xC0) != 0x80) return -1;
|
||||
if ((bytes[i] & 0xC0) != 0x80) {
|
||||
// Bad character. Drop a byte and return invalid CP.
|
||||
source.remove_prefix(1);
|
||||
return 1;
|
||||
}
|
||||
codepoint = (codepoint << 6) | (bytes[i] & 0x3F);
|
||||
}
|
||||
|
||||
@@ -339,7 +346,9 @@ int32_t read_codepoint_utf8(string_view &source) {
|
||||
((codepoint >= 0x0080) && (codepoint <= 0x07FF) && (seqlen != 2)) ||
|
||||
((codepoint >= 0x0800) && (codepoint <= 0xFFFF) && (seqlen != 3)) ||
|
||||
((codepoint >= 0x10000) && (codepoint <= 0x1FFFFF) && (seqlen != 4))) {
|
||||
return -1;
|
||||
// Bad character. Drop a byte and return invalid CP.
|
||||
source.remove_prefix(1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
source.remove_prefix(seqlen);
|
||||
|
||||
Reference in New Issue
Block a user