Skip to content

Commit

Permalink
refactor: make termios non-mandatory in libcartesi (unsupported by WASI)
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Nov 6, 2023
1 parent 88bf574 commit 7043b8c
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/tty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>

#ifndef NO_TERMIOS
#include <termios.h>
#endif

#include "tty.h"

namespace cartesi {
Expand All @@ -34,13 +37,16 @@ static const int CONSOLE_BUF_SIZE = 1024; ///< Number of characters in console i
/// \brief TTY global state
struct tty_state {
bool initialized{false};
int ttyfd{-1};
termios oldtty{};
std::array<char, CONSOLE_BUF_SIZE> buf{};
ssize_t buf_pos{};
ssize_t buf_len{};
#ifndef NO_TERMIOS
int ttyfd{-1};
termios oldtty{};
#endif
};

#ifndef NO_TERMIOS
static int new_ttyfd(const char *path) {
int fd{};
do {
Expand All @@ -66,6 +72,7 @@ static int get_ttyfd(void) {
// NOLINTEND(bugprone-assignment-in-if-condition)
return -1;
}
#endif

static bool try_read_chars_from_stdin(uint64_t wait, char *data, size_t max_len, long *actual_len) {
const int fd_max{0};
Expand Down Expand Up @@ -98,6 +105,7 @@ void tty_initialize(void) {
throw std::runtime_error("TTY already initialized.");
}
s->initialized = true;
#ifndef NO_TERMIOS
// NOLINTNEXTLINE(bugprone-assignment-in-if-condition)
if ((s->ttyfd = get_ttyfd()) >= 0) {
struct termios tty {};
Expand Down Expand Up @@ -129,6 +137,7 @@ void tty_initialize(void) {
tcsetattr(s->ttyfd, TCSANOW, &tty);
//??D Should we check to see if changes stuck?
}
#endif
}

void tty_finalize(void) {
Expand All @@ -137,11 +146,13 @@ void tty_finalize(void) {
throw std::runtime_error("TTY not initialized");
}
s->initialized = false;
#ifndef NO_TERMIOS
if (s->ttyfd >= 0) {
tcsetattr(s->ttyfd, TCSANOW, &s->oldtty);
close(s->ttyfd);
s->ttyfd = -1;
}
#endif
}

void tty_poll_console(uint64_t wait) {
Expand Down

0 comments on commit 7043b8c

Please sign in to comment.