The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit b22b7d393f63f85307ba5bcf6e25bdf7f144b7b7
parent a14867199f35d146308ca98da6f9a29298a70ff5
Author: Alexey Yerin <yyp@disroot.org>
Date:   Tue, 31 Aug 2021 23:07:30 +0300

unix::tty: remove stdout fall-back for open()

This approach has a bunch of major flaws that only exist in that code
path and may cause issues:

* /dev/tty is bi-directional, while stdout is write-only and would yield
  errors::unsupported when read from. Also, the documentation was
  incorect because it was saying it opens stdin.
* Stdout is not unwrapped from buffered stream, and buffering could
  cause a lot of problems when, for example, using escape sequences to
  move the cursor and not flushing the stream after it.
* When /dev/tty is used, the caller needs to io::close it to prevent
  leaks. But after closing stdout, it's gone forever and this may cause
  unexpected breakages when something is printed after finishing TTY

Signed-off-by: Alexey Yerin <yyp@disroot.org>

Munix/tty/+linux/open.ha | 16++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/unix/tty/+linux/open.ha b/unix/tty/+linux/open.ha @@ -3,16 +3,12 @@ use fs; use io; use os; -// Return either /dev/tty or stdin, whichever is accessible and is a tty. +// Returns a stream connected to the TTY of the current process. The caller must +// close it using [[io::close]]. export fn open() (*io::stream | error) = { - match (os::open("/dev/tty", fs::flags::RDWR, fs::flags::CLOEXEC)) { - i: *io::stream => return i, - fs::error => void, + return match (os::open("/dev/tty", fs::flags::RDWR, + fs::flags::CLOEXEC)) { + s: *io::stream => s, + fs::error => errors::noentry, }; - - if (isatty(os::stdout)) { - return os::stdout; - }; - - return errors::noentry; };