commit c14a0bcf6698cbc99621948c4e3fbf523d55f60f
parent c635e2c5e9c843fa303c0ff300ce42b1b09dfdb8
Author: Kiƫd Llaentenn <kiedtl@tilde.team>
Date: Wed, 21 Apr 2021 18:27:53 +0000
Add unix::tty::winsize
This commit adds the unix::tty::winsize function, as well as the
unix::tty::ttysize struct that it returns. Additionally, in
unix::tty::types.ha, an error type was defined.
Diffstat:
4 files changed, 44 insertions(+), 3 deletions(-)
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -653,7 +653,9 @@ unix_passwd() {
unix_tty() {
gen_srcs unix::tty \
- isatty.ha
+ isatty.ha \
+ types.ha \
+ '$(PLATFORM)/winsize.ha'
gen_ssa unix::tty rt io os
}
diff --git a/stdlib.mk b/stdlib.mk
@@ -856,7 +856,9 @@ $(HARECACHE)/unix/passwd/unix_passwd.ssa: $(stdlib_unix_passwd_srcs) $(stdlib_rt
# unix::tty
stdlib_unix_tty_srcs= \
- $(STDLIB)/unix/tty/isatty.ha
+ $(STDLIB)/unix/tty/isatty.ha \
+ $(STDLIB)/unix/tty/types.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)
@printf 'HAREC \t$@\n'
@@ -1753,7 +1755,9 @@ $(TESTCACHE)/unix/passwd/unix_passwd.ssa: $(testlib_unix_passwd_srcs) $(testlib_
# unix::tty
testlib_unix_tty_srcs= \
- $(STDLIB)/unix/tty/isatty.ha
+ $(STDLIB)/unix/tty/isatty.ha \
+ $(STDLIB)/unix/tty/types.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)
@printf 'HAREC \t$@\n'
diff --git a/unix/tty/+linux/winsize.ha b/unix/tty/+linux/winsize.ha
@@ -0,0 +1,27 @@
+use errors;
+use io;
+use os;
+use rt;
+
+// Returns an winsize struct containing the dimensions of underlying fd of the
+// stream.
+export fn winsize(tty: *io::stream) (ttysize | error) = {
+ let fd = match (os::streamfd(tty, true)) {
+ f: int => f,
+ _: void => return errors::invalid,
+ };
+ let wsz = rt::winsize { ... };
+ return match (rt::ioctl(fd, rt::TIOCGWINSZ, &wsz: *void)) {
+ e: rt::errno => {
+ switch (e: int) {
+ rt::EBADFD => errors::invalid,
+ rt::ENOTTY => errors::unsupported,
+ * => abort("unreachable"),
+ };
+ },
+ _: int => ttysize {
+ rows = wsz.ws_row,
+ columns = wsz.ws_col,
+ },
+ };
+};
diff --git a/unix/tty/types.ha b/unix/tty/types.ha
@@ -0,0 +1,8 @@
+use errors;
+
+export type error = (errors::invalid | errors::unsupported)!;
+
+export type ttysize = struct {
+ rows: u16,
+ columns: u16,
+};