Implement unicode on console, move readline into driver

This commit is contained in:
2023-05-18 17:14:55 -04:00
parent 2b03ca2eb6
commit fd137e8e74
12 changed files with 371 additions and 162 deletions

View File

@@ -54,6 +54,7 @@ class Driver {
CHAN_SSL_ACCEPTING,
CHAN_SSL_READWRITE,
};
struct ChanInfo {
int chid;
SOCKET socket;
@@ -89,6 +90,7 @@ class Driver {
bool read_console_recently_;
std::unique_ptr<struct pollfd[]> pollvec_;
std::unique_ptr<char[]> chbuf_;
ReadlineDevice readline_device_;
sslutil::UniqueCTX ssl_server_ctx_;
sslutil::UniqueCTX ssl_client_secure_ctx_;
@@ -202,20 +204,27 @@ class Driver {
engw.get_outgoing(&engw, 0, &ndata, &data);
if (ndata == 0) break;
if (ndata > DRV_SHORTSTRING_SIZE) ndata = DRV_SHORTSTRING_SIZE;
int nwrote = console_write(data, ndata);
if (nwrote <= 0) break;
engw.play_sent_outgoing(&engw, 0, nwrote);
std::string_view src(data, ndata);
int consumed;
CodepointString cps = ReadlineDevice::from_utf8(src, &consumed);
readline_device_.print(cps);
engw.play_sent_outgoing(&engw, 0, consumed);
}
}
void handle_console_input() {
char buffer[256];
read_console_recently_ = false;
while (true) {
int nread = console_read(buffer, 256);
if (nread <= 0) break;
CodepointString cps = console_read();
if (cps.size() == 0) break;
read_console_recently_ = true;
engw.play_recv_incoming(&engw, 0, nread, buffer);
for (char32_t c : cps) {
CodepointString line = readline_device_.putcode(c);
if (!line.empty()) {
std::string utf8 = ReadlineDevice::to_utf8(line);
engw.play_recv_incoming(&engw, 0, utf8.size(), utf8.c_str());
}
}
}
}
@@ -575,6 +584,9 @@ class Driver {
}
int drive(int argc, char *argv[]) {
// Set up the console readline device.
readline_device_.set_print_callback(console_write);
// Remove the program name from argv.
std::string program = argv[0];
argc -= 1;