sketch for the termios library

This commit is contained in:
igor
2026-03-23 10:00:10 +01:00
parent 44a49d17d5
commit 9fbba4eefb
4 changed files with 61 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
stdin is a tty?
1
stdout is a tty?
0
stderr is a tty?
0
Invalid fd (999) is a tty?
-1

View File

@@ -0,0 +1,16 @@
import stdlib.sl
import termios.sl
word main
"stdin is a tty? " puts
0 isatty puti cr
"stdout is a tty? " puts
1 isatty puti cr
"stderr is a tty? " puts
2 isatty puti cr
"Invalid fd (999) is a tty? " puts
999 isatty puti cr
end

36
stdlib/termios.sl Normal file
View File

@@ -0,0 +1,36 @@
import stdlib.sl
macro TCGETS 0 0x5401 ;
macro ENOTTY 0 25 ;
macro EBADF 0 9 ;
# isatty [* | fd] -> [* | flag]
word isatty
>r # save fd
60 alloc # push addr
r@ TCGETS over 3 16 syscall # addr result
# Duplicate result and save it
dup >r # push result to return stack
# Free buffer
60 swap free # free(addr, size)
# Restore result
r> # result back to data stack
# Check result
dup ENOTTY neg == if # -ENOTTY (not a tty)
drop 0
else
dup EBADF neg == if # -EBADF (bad fd)
drop -1
else
# Any other value means it's a tty
drop 1
end
end
rdrop
end

View File

@@ -28,6 +28,7 @@ DEFAULT_EXTRA_TESTS = [
"extra_tests/c_extern_structs.sl",
"extra_tests/fn_test.sl",
"extra_tests/nob_test.sl",
"extra_tests/termios_test.sl",
]
COLORS = {