termios.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //go:build !windows && !plan9
  2. // Copyright 2015 go-termios Author. All Rights Reserved.
  3. // https://github.com/go-termios/termios
  4. // Author: John Lenton <chipaca@github.com>
  5. package eunix
  6. import (
  7. "unsafe"
  8. "golang.org/x/sys/unix"
  9. )
  10. // Termios represents terminal attributes.
  11. type Termios unix.Termios
  12. // TermiosForFd returns a pointer to a Termios structure if the file
  13. // descriptor is open on a terminal device.
  14. func TermiosForFd(fd int) (*Termios, error) {
  15. term, err := unix.IoctlGetTermios(fd, getAttrIOCTL)
  16. return (*Termios)(term), err
  17. }
  18. // ApplyToFd applies term to the given file descriptor.
  19. func (term *Termios) ApplyToFd(fd int) error {
  20. return unix.IoctlSetTermios(fd, setAttrNowIOCTL, (*unix.Termios)(unsafe.Pointer(term)))
  21. }
  22. // Copy returns a copy of term.
  23. func (term *Termios) Copy() *Termios {
  24. v := *term
  25. return &v
  26. }
  27. // SetVTime sets the timeout in deciseconds for noncanonical read.
  28. func (term *Termios) SetVTime(v uint8) {
  29. term.Cc[unix.VTIME] = v
  30. }
  31. // SetVMin sets the minimal number of characters for noncanonical read.
  32. func (term *Termios) SetVMin(v uint8) {
  33. term.Cc[unix.VMIN] = v
  34. }
  35. // SetICanon sets the canonical flag.
  36. func (term *Termios) SetICanon(v bool) {
  37. setFlag(&term.Lflag, unix.ICANON, v)
  38. }
  39. // SetIExten sets the iexten flag.
  40. func (term *Termios) SetIExten(v bool) {
  41. setFlag(&term.Lflag, unix.IEXTEN, v)
  42. }
  43. // SetEcho sets the echo flag.
  44. func (term *Termios) SetEcho(v bool) {
  45. setFlag(&term.Lflag, unix.ECHO, v)
  46. }
  47. // SetICRNL sets the CRNL iflag bit
  48. func (term *Termios) SetICRNL(v bool) {
  49. setFlag(&term.Iflag, unix.ICRNL, v)
  50. }
  51. func setFlag(flag *termiosFlag, mask termiosFlag, v bool) {
  52. if v {
  53. *flag |= mask
  54. } else {
  55. *flag &= ^mask
  56. }
  57. }