
(c-system-include "termios.h")
(c-system-include "sys/ioctl.h")

(define-c-struct termios
  predicate: term-attrs?
  constructor: (make-term-attrs)
  (unsigned-long c_iflag term-attrs-iflag term-attrs-iflag-set!)
  (unsigned-long c_oflag term-attrs-oflag term-attrs-oflag-set!)
  (unsigned-long c_cflag term-attrs-cflag term-attrs-cflag-set!)
  (unsigned-long c_lflag term-attrs-lflag term-attrs-lflag-set!)
  ;;(unsigned-char (c_cc 22) term-attrs-cc term-attrs-cc-set!)
  ;;(unsigned-long c_ispeed term-attrs-ispeed term-attrs-ispeed-set!)
  ;;(unsigned-long c_ospeed term-attrs-ospeed term-attrs-ospeed-set!)
  )

(cond-expand
  (emscripten)
  (else
    (define-c unsigned-long (term-attrs-ispeed cfgetispeed) (termios))
    (define-c unsigned-long (term-attrs-ospeed cfgetospeed) (termios))
    (define-c errno (term-attrs-ispeed-set! cfsetispeed) (termios unsigned-long))
    (define-c errno (term-attrs-ospeed-set! cfsetospeed) (termios unsigned-long))))

(define-c-struct winsize
  predicate: winsize?
  constructor: (make-winsize ws_row ws_col)
  (unsigned-short ws_row winsize-row)
  (unsigned-short ws_col winsize-col))

(define-c errno ioctl (port-or-fileno unsigned-long (result winsize)))

(define-c-const int TIOCGWINSZ)

(define-c-const int TCSANOW)
(define-c-const int TCSADRAIN)
(define-c-const int TCSAFLUSH)

(define-c-const unsigned-long IGNBRK)
(define-c-const unsigned-long BRKINT)
(define-c-const unsigned-long IGNPAR)
(define-c-const unsigned-long PARMRK)
(define-c-const unsigned-long INPCK)
(define-c-const unsigned-long ISTRIP)
(define-c-const unsigned-long INLCR)
(define-c-const unsigned-long IGNCR)
(define-c-const unsigned-long ICRNL)
(define-c-const unsigned-long IXON)
(define-c-const unsigned-long IXOFF)
(define-c-const unsigned-long IXANY)
(define-c-const unsigned-long IMAXBEL)
;; (define-c-const unsigned-long IUCLC)

(define-c-const unsigned-long OPOST)
(define-c-const unsigned-long ONLCR)
;; (define-c-const unsigned-long OXTABS)
;; (define-c-const unsigned-long ONOEOT)
(define-c-const unsigned-long OCRNL)
;; (define-c-const unsigned-long OLCUC)
(define-c-const unsigned-long ONOCR)
(define-c-const unsigned-long ONLRET)

(define-c-const unsigned-long CSIZE)
(define-c-const unsigned-long CS5)
(define-c-const unsigned-long CS6)
(define-c-const unsigned-long CS7)
(define-c-const unsigned-long CS8)
(define-c-const unsigned-long CSTOPB)
(define-c-const unsigned-long CREAD)
(define-c-const unsigned-long PARENB)
(define-c-const unsigned-long PARODD)
(define-c-const unsigned-long HUPCL)
(define-c-const unsigned-long CLOCAL)
;; (define-c-const unsigned-long CCTS_OFLOW)
(define-c-const unsigned-long CRTSCTS)
;; (define-c-const unsigned-long CRTS_IFLOW)
;; (define-c-const unsigned-long MDMBUF)

(define-c-const unsigned-long ECHOKE)
(define-c-const unsigned-long ECHOE)
(define-c-const unsigned-long ECHO)
(define-c-const unsigned-long ECHONL)
;; (define-c-const unsigned-long ECHOPRT)
(define-c-const unsigned-long ECHOCTL)
(define-c-const unsigned-long ISIG)
(define-c-const unsigned-long ICANON)
;; (define-c-const unsigned-long ALTWERASE)
(define-c-const unsigned-long IEXTEN)
;; (define-c-const unsigned-long EXTPROC)
(define-c-const unsigned-long TOSTOP)
(define-c-const unsigned-long FLUSHO)
;; (define-c-const unsigned-long NOKERNINFO)
;; (define-c-const unsigned-long PENDIN)
(define-c-const unsigned-long NOFLSH)

(define-c-const unsigned-long VEOF)
(define-c-const unsigned-long VEOL)
(define-c-const unsigned-long VEOL2)
(define-c-const unsigned-long VERASE)
;; (define-c-const unsigned-long VERASE2)
(define-c-const unsigned-long VWERASE)
(define-c-const unsigned-long VINTR)
(define-c-const unsigned-long VKILL)
(define-c-const unsigned-long VQUIT)
(define-c-const unsigned-long VSUSP)
(define-c-const unsigned-long VSTART)
(define-c-const unsigned-long VSTOP)
;; (define-c-const unsigned-long VDSUSP)
(define-c-const unsigned-long VLNEXT)
(define-c-const unsigned-long VREPRINT)
;; (define-c-const unsigned-long VSTATUS)

(define-c errno (get-terminal-attributes "tcgetattr")
  (port-or-fileno (result termios)))
(define-c errno (set-terminal-attributes! "tcsetattr")
  (port-or-fileno int termios))
