commit c279d8a45b837e335386cb1abbb8d96a37387bdf
parent 74f7f6c412cb701802166fe3a9fb3457497a5b58
Author: Kiƫd Llaentenn <kiedtl@tilde.team>
Date: Wed, 21 Apr 2021 16:08:51 +0000
Add unix::tty::isatty
In addition to adding unix::tty::isatty, this commit also adds
rt::winsize and rt::TIOCGWINSZ; both of which were necessary for the
ioctl calls.
Diffstat:
4 files changed, 58 insertions(+), 0 deletions(-)
diff --git a/rt/+linux/types.ha b/rt/+linux/types.ha
@@ -556,3 +556,12 @@ export type cmsg = struct {
export def PRIO_PROCESS: int = 0;
export def PRIO_PGRP: int = 1;
export def PRIO_USER: int = 2;
+
+export type winsize = struct {
+ ws_row: u16,
+ ws_col: u16,
+ ws_xpixel: u16,
+ ws_ypixel: u16,
+};
+
+export def TIOCGWINSZ: u64 = 0x5413;
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -651,6 +651,12 @@ unix_passwd() {
gen_ssa unix::passwd bufio io os strconv strings
}
+unix_tty() {
+ gen_srcs unix::tty \
+ isatty.ha
+ gen_ssa unix::tty rt io os
+}
+
uuid() {
gen_srcs uuid \
uuid.ha
@@ -711,6 +717,7 @@ types
unicode
unix
unix_passwd
+unix_tty
uuid"
stdlib() {
rt
diff --git a/stdlib.mk b/stdlib.mk
@@ -225,6 +225,9 @@ hare_stdlib_deps+=$(stdlib_unix)
stdlib_unix_passwd=$(HARECACHE)/unix/passwd/unix_passwd.o
hare_stdlib_deps+=$(stdlib_unix_passwd)
+stdlib_unix_tty=$(HARECACHE)/unix/tty/unix_tty.o
+hare_stdlib_deps+=$(stdlib_unix_tty)
+
stdlib_uuid=$(HARECACHE)/uuid/uuid.o
hare_stdlib_deps+=$(stdlib_uuid)
@@ -851,6 +854,16 @@ $(HARECACHE)/unix/passwd/unix_passwd.ssa: $(stdlib_unix_passwd_srcs) $(stdlib_rt
@HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nunix::passwd \
-t$(HARECACHE)/unix/passwd/unix_passwd.td $(stdlib_unix_passwd_srcs)
+# unix::tty
+stdlib_unix_tty_srcs= \
+ $(STDLIB)/unix/tty/isatty.ha
+
+$(HARECACHE)/unix/tty/unix_tty.ssa: $(stdlib_unix_tty_srcs) $(stdlib_rt) $(stdlib_rt) $(stdlib_io) $(stdlib_os)
+ @printf 'HAREC \t$@\n'
+ @mkdir -p $(HARECACHE)/unix/tty
+ @HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nunix::tty \
+ -t$(HARECACHE)/unix/tty/unix_tty.td $(stdlib_unix_tty_srcs)
+
# uuid
stdlib_uuid_srcs= \
$(STDLIB)/uuid/uuid.ha
@@ -1089,6 +1102,9 @@ hare_testlib_deps+=$(testlib_unix)
testlib_unix_passwd=$(TESTCACHE)/unix/passwd/unix_passwd.o
hare_testlib_deps+=$(testlib_unix_passwd)
+testlib_unix_tty=$(TESTCACHE)/unix/tty/unix_tty.o
+hare_testlib_deps+=$(testlib_unix_tty)
+
testlib_uuid=$(TESTCACHE)/uuid/uuid.o
hare_testlib_deps+=$(testlib_uuid)
@@ -1735,6 +1751,16 @@ $(TESTCACHE)/unix/passwd/unix_passwd.ssa: $(testlib_unix_passwd_srcs) $(testlib_
@HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nunix::passwd \
-t$(TESTCACHE)/unix/passwd/unix_passwd.td $(testlib_unix_passwd_srcs)
+# unix::tty
+testlib_unix_tty_srcs= \
+ $(STDLIB)/unix/tty/isatty.ha
+
+$(TESTCACHE)/unix/tty/unix_tty.ssa: $(testlib_unix_tty_srcs) $(testlib_rt) $(testlib_rt) $(testlib_io) $(testlib_os)
+ @printf 'HAREC \t$@\n'
+ @mkdir -p $(TESTCACHE)/unix/tty
+ @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nunix::tty \
+ -t$(TESTCACHE)/unix/tty/unix_tty.td $(testlib_unix_tty_srcs)
+
# uuid
testlib_uuid_srcs= \
$(STDLIB)/uuid/uuid.ha
diff --git a/unix/tty/isatty.ha b/unix/tty/isatty.ha
@@ -0,0 +1,16 @@
+use rt;
+use io;
+use os;
+
+// Returns true if the file descriptor underlying a stream is a terminal.
+export fn isatty(stream: *io::stream) bool = {
+ let fd = match (os::streamfd(stream, true)) {
+ f: int => f,
+ _: void => return false,
+ };
+ let wsz = rt::winsize { ... };
+ return match (rt::ioctl(fd, rt::TIOCGWINSZ, &wsz: *void)) {
+ e: rt::errno => false,
+ r: int => if (r == 0) true else false,
+ };
+};