Figured it out.
;; read-stdin.scm
(use posix)
;; let me know if STDIN is coming from a terminal or a pipe/file
(if (terminal-port? (current-input-port))
(fprintf (current-error-port) "~A~%" "stdin is a terminal") ;; prints to stderr
(fprintf (current-error-port) "~A~%" "stdin is a pipe or file"))
;; read from STDIN
(do ((c (read-char) (read-char)))
((eof-object? c))
(printf "~C" c))
(newline)
According to the Chicken wiki, terminal-port?
is Chicken's equivalent to C's isatty()
function.
NOTE
The above example works best when compiled. Running it with csi
seems to make terminal-port?
always return true, but perhaps adding an explicit call to (exit)
and th end of the file would cause the Chicken Scheme interpreter to exit, thus allowing STDIN
to be something other than a terminal?