diff --git a/scsh/tty.c b/scsh/tty.c index ad2fced..205e17c 100644 --- a/scsh/tty.c +++ b/scsh/tty.c @@ -17,6 +17,7 @@ extern int errno; #define errno_or_false(x) (((x) == -1) ? ENTER_FIXNUM(errno) : SCHFALSE) +#define errno_on_zero_or_false(x) ((x) ? SCHFALSE : ENTER_FIXNUM(errno)) scheme_value df_scheme_tcgetattr(long nargs, scheme_value *args) { extern int scheme_tcgetattr(int , char *, int *, int *, int *, int *, int *, int *, int *, int *, int *, int *); @@ -147,3 +148,41 @@ scheme_value df_open_ctty(long nargs, scheme_value *args) return ret1; } +scheme_value df_isatty(long nargs, scheme_value *args) +{ + extern int isatty(int ); + scheme_value ret1; + int r1; + + cig_check_nargs(1, nargs, "isatty"); + r1 = isatty(EXTRACT_FIXNUM(args[0])); + ret1 = ENTER_BOOLEAN(r1); + return ret1; + } + +scheme_value df_ttyname(long nargs, scheme_value *args) +{ + extern char *ttyname(int ); + scheme_value ret1; + char *r1; + + cig_check_nargs(2, nargs, "ttyname"); + r1 = ttyname(EXTRACT_FIXNUM(args[1])); + ret1 = errno_on_zero_or_false(r1); + {AlienVal(CAR(VECTOR_REF(*args,0))) = (long) r1; CDR(VECTOR_REF(*args,0)) = strlen_or_false(r1);} + return ret1; + } + +scheme_value df_scm_ctermid(long nargs, scheme_value *args) +{ + extern char *scm_ctermid(void); + scheme_value ret1; + char *r1; + + cig_check_nargs(1, nargs, "scm_ctermid"); + r1 = scm_ctermid(); + ret1 = errno_on_zero_or_false(r1); + {AlienVal(CAR(VECTOR_REF(*args,0))) = (long) r1; CDR(VECTOR_REF(*args,0)) = strlen_or_false(r1);} + return ret1; + } + diff --git a/scsh/tty.scm b/scsh/tty.scm index 8bc4402..3e6bda4 100644 --- a/scsh/tty.scm +++ b/scsh/tty.scm @@ -25,6 +25,7 @@ "extern int errno;" "" "#define errno_or_false(x) (((x) == -1) ? ENTER_FIXNUM(errno) : SCHFALSE)" + "#define errno_on_zero_or_false(x) ((x) ? SCHFALSE : ENTER_FIXNUM(errno))" "" ) @@ -362,3 +363,27 @@ (multi-rep (to-scheme integer errno_or_false) integer)) + +;;; Random bits & pieces: isatty ttyname ctermid +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; (tty? fd/port) -> boolean +;;; (tty-file-name fd/port) -> string +;;; (control-tty-file-name) -> string + +(define-foreign %tty? (isatty (integer fd)) bool) +(define (tty? fd/port) (sleazy-call/fdes fd/port %tty?)) + + +(define-foreign %tty-file-name/errno (ttyname (integer fd)) + (multi-rep (to-scheme static-string errno_on_zero_or_false) + static-string)) +(define-errno-syscall (%tty-file-name fd) %tty-file-name/errno + tty-name) +(define (tty-file-name fd/port) (sleazy-call/fdes fd/port %tty-file-name)) + + +(define-foreign %ctermid/errno (scm_ctermid) + (multi-rep (to-scheme static-string errno_on_zero_or_false) + static-string)) + +(define-errno-syscall (control-tty-file-name) %ctermid/errno term-name) diff --git a/scsh/tty1.c b/scsh/tty1.c index 7cd7b60..0b34c85 100644 --- a/scsh/tty1.c +++ b/scsh/tty1.c @@ -12,6 +12,7 @@ */ #include +#include /* ctermid decl */ #include #include #include @@ -115,3 +116,5 @@ int open_ctty(const char *ttyname, int flags) #endif return fd; } + +char *scm_ctermid() { return ctermid(0); } diff --git a/scsh/tty1.h b/scsh/tty1.h index 717b4e4..3ea61f7 100644 --- a/scsh/tty1.h +++ b/scsh/tty1.h @@ -17,3 +17,5 @@ int scheme_tcsetattr(int fd, int option, int min, int time); int open_ctty(const char *ttyname, int flags); + +char *scm_ctermid(void);