sketch for the termios library
This commit is contained in:
8
extra_tests/termios_test.expected
Normal file
8
extra_tests/termios_test.expected
Normal 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
|
||||||
16
extra_tests/termios_test.sl
Normal file
16
extra_tests/termios_test.sl
Normal 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
36
stdlib/termios.sl
Normal 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
|
||||||
Reference in New Issue
Block a user