termios_bsd.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // +build darwin freebsd openbsd
  2. package term
  3. import (
  4. "unsafe"
  5. "golang.org/x/sys/unix"
  6. )
  7. const (
  8. getTermios = unix.TIOCGETA
  9. setTermios = unix.TIOCSETA
  10. )
  11. // Termios is the Unix API for terminal I/O.
  12. type Termios unix.Termios
  13. // MakeRaw put the terminal connected to the given file descriptor into raw
  14. // mode and returns the previous state of the terminal so that it can be
  15. // restored.
  16. func MakeRaw(fd uintptr) (*State, error) {
  17. var oldState State
  18. if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
  19. return nil, err
  20. }
  21. newState := oldState.termios
  22. newState.Iflag &^= (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON)
  23. newState.Oflag &^= unix.OPOST
  24. newState.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN)
  25. newState.Cflag &^= (unix.CSIZE | unix.PARENB)
  26. newState.Cflag |= unix.CS8
  27. newState.Cc[unix.VMIN] = 1
  28. newState.Cc[unix.VTIME] = 0
  29. if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 {
  30. return nil, err
  31. }
  32. return &oldState, nil
  33. }