diff --git a/extra_tests/termios_test.expected b/extra_tests/termios_test.expected new file mode 100644 index 0000000..ede3aff --- /dev/null +++ b/extra_tests/termios_test.expected @@ -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 diff --git a/extra_tests/termios_test.sl b/extra_tests/termios_test.sl new file mode 100644 index 0000000..c9c50a6 --- /dev/null +++ b/extra_tests/termios_test.sl @@ -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 diff --git a/stdlib/termios.sl b/stdlib/termios.sl new file mode 100644 index 0000000..4f2b641 --- /dev/null +++ b/stdlib/termios.sl @@ -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 diff --git a/test.py b/test.py index 3ee45b0..b14c7b3 100644 --- a/test.py +++ b/test.py @@ -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 = {