commit d4b4305b9f0a8b38282321ace5b34d6eb8cacf12
parent 6011389265860e02cdf71870ae5e5ba7aaca8e9c
Author: Kiëd Llaentenn <kiedtl@tilde.team>
Date: Fri, 23 Apr 2021 18:12:48 +0000
Add unix::tty::open
Signed-off-by: Kiëd Llaentenn <kiedtl@tilde.team>
Diffstat:
4 files changed, 28 insertions(+), 7 deletions(-)
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -653,10 +653,11 @@ unix_passwd() {
unix_tty() {
gen_srcs unix::tty \
- '$(PLATFORM)/isatty.ha' \
types.ha \
+ '$(PLATFORM)/isatty.ha' \
+ '$(PLATFORM)/open.ha' \
'$(PLATFORM)/winsize.ha'
- gen_ssa unix::tty rt io os
+ gen_ssa unix::tty rt fs io os
}
uuid() {
diff --git a/stdlib.mk b/stdlib.mk
@@ -856,11 +856,12 @@ $(HARECACHE)/unix/passwd/unix_passwd.ssa: $(stdlib_unix_passwd_srcs) $(stdlib_rt
# unix::tty
stdlib_unix_tty_srcs= \
- $(STDLIB)/unix/tty/$(PLATFORM)/isatty.ha \
$(STDLIB)/unix/tty/types.ha \
+ $(STDLIB)/unix/tty/$(PLATFORM)/isatty.ha \
+ $(STDLIB)/unix/tty/$(PLATFORM)/open.ha \
$(STDLIB)/unix/tty/$(PLATFORM)/winsize.ha
-$(HARECACHE)/unix/tty/unix_tty.ssa: $(stdlib_unix_tty_srcs) $(stdlib_rt) $(stdlib_rt) $(stdlib_io) $(stdlib_os)
+$(HARECACHE)/unix/tty/unix_tty.ssa: $(stdlib_unix_tty_srcs) $(stdlib_rt) $(stdlib_rt) $(stdlib_fs) $(stdlib_io) $(stdlib_os)
@printf 'HAREC \t$@\n'
@mkdir -p $(HARECACHE)/unix/tty
@HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nunix::tty \
@@ -1755,11 +1756,12 @@ $(TESTCACHE)/unix/passwd/unix_passwd.ssa: $(testlib_unix_passwd_srcs) $(testlib_
# unix::tty
testlib_unix_tty_srcs= \
- $(STDLIB)/unix/tty/$(PLATFORM)/isatty.ha \
$(STDLIB)/unix/tty/types.ha \
+ $(STDLIB)/unix/tty/$(PLATFORM)/isatty.ha \
+ $(STDLIB)/unix/tty/$(PLATFORM)/open.ha \
$(STDLIB)/unix/tty/$(PLATFORM)/winsize.ha
-$(TESTCACHE)/unix/tty/unix_tty.ssa: $(testlib_unix_tty_srcs) $(testlib_rt) $(testlib_rt) $(testlib_io) $(testlib_os)
+$(TESTCACHE)/unix/tty/unix_tty.ssa: $(testlib_unix_tty_srcs) $(testlib_rt) $(testlib_rt) $(testlib_fs) $(testlib_io) $(testlib_os)
@printf 'HAREC \t$@\n'
@mkdir -p $(TESTCACHE)/unix/tty
@HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nunix::tty \
diff --git a/unix/tty/+linux/open.ha b/unix/tty/+linux/open.ha
@@ -0,0 +1,18 @@
+use errors;
+use fs;
+use io;
+use os;
+
+// Return either /dev/tty or stdin, whichever is accessible and is a tty.
+export fn open() (*io::stream | error) = {
+ match (os::open("/dev/tty")) {
+ i: *io::stream => return i,
+ _: fs::error => void,
+ };
+
+ if (isatty(os::stdout)) {
+ return os::stdout;
+ };
+
+ return errors::noentry;
+};
diff --git a/unix/tty/types.ha b/unix/tty/types.ha
@@ -1,6 +1,6 @@
use errors;
-export type error = (errors::invalid | errors::unsupported)!;
+export type error = (errors::invalid | errors::unsupported | errors::noentry)!;
export type ttysize = struct {
rows: u16,