termios_darwin.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package term
  2. import (
  3. "syscall"
  4. "unsafe"
  5. )
  6. const (
  7. getTermios = syscall.TIOCGETA
  8. setTermios = syscall.TIOCSETA
  9. ECHO = 0x00000008
  10. ONLCR = 0x2
  11. ISTRIP = 0x20
  12. INLCR = 0x40
  13. ISIG = 0x80
  14. IGNCR = 0x80
  15. ICANON = 0x100
  16. ICRNL = 0x100
  17. IXOFF = 0x400
  18. IXON = 0x200
  19. )
  20. type Termios struct {
  21. Iflag uint64
  22. Oflag uint64
  23. Cflag uint64
  24. Lflag uint64
  25. Cc [20]byte
  26. Ispeed uint64
  27. Ospeed uint64
  28. }
  29. // MakeRaw put the terminal connected to the given file descriptor into raw
  30. // mode and returns the previous state of the terminal so that it can be
  31. // restored.
  32. func MakeRaw(fd uintptr) (*State, error) {
  33. var oldState State
  34. if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
  35. return nil, err
  36. }
  37. newState := oldState.termios
  38. newState.Iflag &^= (ISTRIP | INLCR | IGNCR | IXON | IXOFF)
  39. newState.Iflag |= ICRNL
  40. newState.Oflag |= ONLCR
  41. newState.Lflag &^= (ECHO | ICANON | ISIG)
  42. if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(setTermios), uintptr(unsafe.Pointer(&newState))); err != 0 {
  43. return nil, err
  44. }
  45. return &oldState, nil
  46. }