Skip to content

Commit

Permalink
fix: terminal raw-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
diegonehab committed Sep 13, 2023
1 parent cb6f039 commit 47e7795
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/tty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,24 @@ void tty_initialize(void) {
s->oldtty = tty;
// Set terminal to "raw" mode
tty.c_lflag &= ~(ECHO | // Echo off
ICANON | // Canonical mode off
ECHONL | // Do not echo NL (redundant with ECHO and ICANON)
ISIG | // Signal chars off
IEXTEN // Extended input processing off
ICANON | // Canonical mode off (input byte-by-byte, no input processing, disable line editing)
ISIG | // Signal chars off (e.g., CTRL+C and CTRL+Z)
IEXTEN | // Extended input processing off (e.g. disable CTRL+V processing)
ECHONL // Do not echo NL despite ECHO being off
);
tty.c_iflag &= ~(IGNBRK | // Generate \377 \0 \0 on BREAK
BRKINT | //
PARMRK | //
ICRNL | // No CR-to-NL
tty.c_iflag &= ~(BRKINT | // --
PARMRK | // Generate \377 \0 \0 on BREAK
IGNBRK | // --
ICRNL | // No CR-to-NL (i.e., typing ENTER CR does not become LF)
INPCK | // Disable parity checking
ISTRIP | // Do not strip off 8th bit
IXON | // Disable software flow control (e.g. CTRL+S XOFF and CTRL+Q XON)
INLCR | // No NL-to-CR
IGNCR | // Do not ignore CR
IXON // Disable XON/XOFF flow control on output
IGNCR // Do not ignore CR
);
tty.c_oflag |= OPOST; // Enable output processing
// Enable parity generation on output and checking for input
tty.c_cflag &= ~(CSIZE | PARENB);
tty.c_cflag |= CS8;
tty.c_oflag &= ~(OPOST); // Disable output processing (e.g., LF -> CR LF)

This comment has been minimized.

Copy link
@edubart

edubart Oct 7, 2023

Contributor

I was testing this commit.

Disabling OPOST (output processing) will disable LR -> CR LF conversion, and this fixes the problem here, so I agree that we could disable OPOST. But the side effect of this is disabling \r even for printfs outside the interpreter, meaning all printfs (including from machine.cpp, cartesi-machine-lua, etc) will not have \r on every \n and not return to line beginning in the terminal, making all printfs outside the machine messy.

I see two possible solutions for this side effect:

  1. Use \r\n in all printfs outside the machine, I think this is too much rewrite and a pain.
  2. Disable OPOST only while interpreting (using tcsetattr before and after interpret). I did not test this though, but then "printf" debugging and our DUMPs while interpreting becomes a little more painful, they all need to use \r\n. Or we could be more aggressive and disable/enable OPOST on every tty_putchar.

This comment has been minimized.

Copy link
@diegonehab

diegonehab Feb 28, 2024

Author Contributor

This stuff is so confusing. Even if we pipe the output of the emulator to a file, and the emulator is not in interactive mode, we end up getting \r\r\n in the output.

tty.c_cflag |= CS8; // Set char size to 8 bits
tty.c_cflag &= ~(PARENB); // Disable parity generation on output and checking on input
// Read returns with 1 char and no delay
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
Expand Down

0 comments on commit 47e7795

Please sign in to comment.