commit 7bada30df0f32f5bdf2b9ba9900b92c22d1eaf2e
parent 27d0f5dedfe576c06c97cca13227a076461ca913
Author: Lorenz (xha) <me@xha.li>
Date: Sat, 25 Nov 2023 15:18:01 +0100
unix::tty: make openpty() platform-specific
Signed-off-by: Lorenz (xha) <me@xha.li>
Diffstat:
4 files changed, 58 insertions(+), 45 deletions(-)
diff --git a/unix/tty/+freebsd/pty.ha b/unix/tty/+freebsd/pty.ha
@@ -9,6 +9,20 @@ use os;
use rt;
use types::c;
+// Opens an available pseudoterminal and returns the file descriptors of the
+// master and slave.
+export fn openpty() ((io::file, io::file) | fs::error) = {
+ let master = open_master()?;
+ let slave = match (get_slave(master)) {
+ case let e: fs::error =>
+ io::close(master)!;
+ return e;
+ case let s: io::file =>
+ yield s;
+ };
+ return (master, slave);
+};
+
// Opens an available pseudoterminal master.
fn open_master() (io::file | fs::error) = {
match (rt::posix_openpt(rt::O_RDWR | rt::O_NOCTTY)) {
diff --git a/unix/tty/+linux/pty.ha b/unix/tty/+linux/pty.ha
@@ -8,6 +8,20 @@ use io;
use os;
use rt;
+// Opens an available pseudoterminal and returns the file descriptors of the
+// master and slave.
+export fn openpty() ((io::file, io::file) | fs::error) = {
+ let master = open_master()?;
+ let slave = match (get_slave(master)) {
+ case let e: fs::error =>
+ io::close(master)!;
+ return e;
+ case let s: io::file =>
+ yield s;
+ };
+ return (master, slave);
+};
+
// Opens an available pseudoterminal master.
fn open_master() (io::file | fs::error) = {
return os::open("/dev/ptmx", fs::flag::RDWR);
diff --git a/unix/tty/pty_common.ha b/unix/tty/pty_common.ha
@@ -1,45 +0,0 @@
-// SPDX-License-Identifier: MPL-2.0
-// (c) Hare authors <https://harelang.org>
-
-use bufio;
-use fmt;
-use fs;
-use io;
-use os;
-use strings;
-
-// Opens an available pseudoterminal and returns the file descriptors of the
-// master and slave.
-export fn openpty() ((io::file, io::file) | fs::error) = {
- let master = open_master()?;
- let slave = match (get_slave(master)) {
- case let e: fs::error =>
- io::close(master)!;
- return e;
- case let s: io::file =>
- yield s;
- };
-
- return (master, slave);
-};
-
-@test fn pty() void = {
- let pty = openpty()!;
- defer io::close(pty.1)!;
- defer io::close(pty.0)!;
-
- assert(fs::exists(os::cwd, ptsname(pty.0)!));
-
- for (let i: u16 = 5; i < 100; i += 1) {
- let sz1 = ttysize { rows = i, columns = i };
- set_winsize(pty.1, sz1)!;
- let sz2 = winsize(pty.1)!;
- assert(sz2.rows == sz1.rows);
- assert(sz2.columns == sz1.columns);
- };
-
- fmt::fprintln(pty.0, "hello, world")!;
- let s = strings::fromutf8(bufio::read_line(pty.1) as []u8)!;
- defer free(s);
- assert(s == "hello, world");
-};
diff --git a/unix/tty/pty_test.ha b/unix/tty/pty_test.ha
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: MPL-2.0
+// (c) Hare authors <https://harelang.org>
+
+use bufio;
+use fmt;
+use fs;
+use io;
+use os;
+use strings;
+
+@test fn pty() void = {
+ let pty = openpty()!;
+ defer io::close(pty.1)!;
+ defer io::close(pty.0)!;
+
+ assert(fs::exists(os::cwd, ptsname(pty.0)!));
+
+ for (let i: u16 = 5; i < 100; i += 1) {
+ let sz1 = ttysize { rows = i, columns = i };
+ set_winsize(pty.1, sz1)!;
+ let sz2 = winsize(pty.1)!;
+ assert(sz2.rows == sz1.rows);
+ assert(sz2.columns == sz1.columns);
+ };
+
+ fmt::fprintln(pty.0, "hello, world")!;
+ let s = strings::fromutf8(bufio::read_line(pty.1) as []u8)!;
+ defer free(s);
+ assert(s == "hello, world");
+};