Struct return value (and pointer?) 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 tcsetattr
  (foreign-lambda int "tcsetattr" int int (c-pointer (struct termios))))

(define cfmakeraw
  (foreign-lambda void "cfmakeraw" (c-pointer (struct termios))))

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