Struct return and arg added by dieggsy 2 days ago

#>
#include "termios.h"
<#

(export call-with-raw-io)

(define tcgetattr
  (foreign-lambda* (struct termios) ((int fd))
    "struct termios termios;
     tcgetattr(fd, &termios);
     C_return(termios);"))

;; (define tcgetattr
;;   (foreign-lambda int "tcgetattr" int (c-pointer (struct termios))))

(define tcsetattr
  (foreign-lambda* int ((int fd) (int actions) ((struct termios) termios))
    "C_return(tcsetattr(fd, actions, &termios));"))

(define cfmakeraw
  (foreign-lambda* void (((struct termios) termios))
    "cfmakeraw(&termios);"))

(define (call-with-raw-io fn #!optional (port (current-input-port)))
  (let* ((fd (chicken:port->fileno port))
         (old (tcgetattr fd))
         (new (tcgetattr fd)))
    (cfmakeraw new)
    (dynamic-wind
        (lambda ()
          (tcsetattr fd (foreign-value "TCSANOW" int) new))
        fn
        (lambda () (tcsetattr fd (foreign-value "TCSANOW" int) old)))))