termios_solaris.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package termios
  2. // #include <termios.h>
  3. // typedef struct termios termios_t;
  4. import "C"
  5. import (
  6. "golang.org/x/sys/unix"
  7. "unsafe"
  8. )
  9. const (
  10. TCSETS = 0x5402
  11. TCSETSW = 0x5403
  12. TCSETSF = 0x5404
  13. TCFLSH = 0x540B
  14. TCSBRK = 0x5409
  15. TCSBRKP = 0x5425
  16. IXON = 0x00000400
  17. IXANY = 0x00000800
  18. IXOFF = 0x00001000
  19. CRTSCTS = 0x80000000
  20. )
  21. // See /usr/include/sys/termios.h
  22. const FIORDCHK = C.FIORDCHK
  23. // Tcgetattr gets the current serial port settings.
  24. func Tcgetattr(fd uintptr, argp *unix.Termios) error {
  25. termios, err := unix.IoctlGetTermios(int(fd), unix.TCGETS)
  26. *argp = *(tiosTounix(termios))
  27. return err
  28. }
  29. // Tcsetattr sets the current serial port settings.
  30. func Tcsetattr(fd, action uintptr, argp *unix.Termios) error {
  31. return unix.IoctlSetTermios(int(fd), uint(action), tiosToUnix(argp))
  32. }
  33. // Tcsendbreak transmits a continuous stream of zero-valued bits for a specific
  34. // duration, if the terminal is using asynchronous serial data transmission. If
  35. // duration is zero, it transmits zero-valued bits for at least 0.25 seconds, and not more that 0.5 seconds.
  36. // If duration is not zero, it sends zero-valued bits for some
  37. // implementation-defined length of time.
  38. func Tcsendbreak(fd, duration uintptr) error {
  39. return ioctl(fd, TCSBRKP, duration)
  40. }
  41. // Tcdrain waits until all output written to the object referred to by fd has been transmitted.
  42. func Tcdrain(fd uintptr) error {
  43. // simulate drain with TCSADRAIN
  44. var attr unix.Termios
  45. if err := Tcgetattr(fd, &attr); err != nil {
  46. return err
  47. }
  48. return Tcsetattr(fd, TCSADRAIN, &attr)
  49. }
  50. // Tcflush discards data written to the object referred to by fd but not transmitted, or data received but not read, depending on the value of selector.
  51. func Tcflush(fd, selector uintptr) error {
  52. return ioctl(fd, TCFLSH, selector)
  53. }
  54. // Tiocinq returns the number of bytes in the input buffer.
  55. func Tiocinq(fd uintptr, argp *int) (err error) {
  56. *argp, err = unix.IoctlGetInt(int(fd), FIORDCHK)
  57. return err
  58. }
  59. // Tiocoutq return the number of bytes in the output buffer.
  60. func Tiocoutq(fd uintptr, argp *int) error {
  61. return ioctl(fd, unix.TIOCOUTQ, uintptr(unsafe.Pointer(argp)))
  62. }
  63. // Cfgetispeed returns the input baud rate stored in the termios structure.
  64. func Cfgetispeed(attr *unix.Termios) uint32 {
  65. solTermios := tiosToUnix(attr)
  66. return uint32(C.cfgetispeed((*C.termios_t)(unsafe.Pointer(solTermios))))
  67. }
  68. // Cfsetispeed sets the input baud rate stored in the termios structure.
  69. func Cfsetispeed(attr *unix.Termios, speed uintptr) error {
  70. solTermios := tiosToUnix(attr)
  71. _, err := C.cfsetispeed((*C.termios_t)(unsafe.Pointer(solTermios)), C.speed_t(speed))
  72. return err
  73. }
  74. // Cfgetospeed returns the output baud rate stored in the termios structure.
  75. func Cfgetospeed(attr *unix.Termios) uint32 {
  76. solTermios := tiosToUnix(attr)
  77. return uint32(C.cfgetospeed((*C.termios_t)(unsafe.Pointer(solTermios))))
  78. }
  79. // Cfsetospeed sets the output baud rate stored in the termios structure.
  80. func Cfsetospeed(attr *unix.Termios, speed uintptr) error {
  81. solTermios := tiosToUnix(attr)
  82. _, err := C.cfsetospeed((*C.termios_t)(unsafe.Pointer(solTermios)), C.speed_t(speed))
  83. return err
  84. }
  85. // tiosToUnix copies a unix.Termios to a x/sys/unix.Termios.
  86. // This is needed since type conversions between the two fail due to
  87. // more recent x/sys/unix.Termios renaming the padding field.
  88. func tiosToUnix(st *unix.Termios) *unix.Termios {
  89. return &unix.Termios{
  90. Iflag: st.Iflag,
  91. Oflag: st.Oflag,
  92. Cflag: st.Cflag,
  93. Lflag: st.Lflag,
  94. Cc: st.Cc,
  95. }
  96. }
  97. // tiosTounix copies a x/sys/unix.Termios to a unix.Termios.
  98. func tiosTounix(ut *unix.Termios) *unix.Termios {
  99. return &unix.Termios{
  100. Iflag: ut.Iflag,
  101. Oflag: ut.Oflag,
  102. Cflag: ut.Cflag,
  103. Lflag: ut.Lflag,
  104. Cc: ut.Cc,
  105. }
  106. }