sys_unix.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2010 Jonas mg
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. // +build !plan9,!windows
  7. // Reference: man termios ; man tty_ioctl
  8. // Linux file: "asm-generic/termbits.h"
  9. package sys
  10. import (
  11. "unsafe"
  12. "golang.org/x/sys/unix"
  13. )
  14. // int tcgetattr(int fd, struct termios *termios_p)
  15. func Getattr(fd int, state *Termios) (err error) {
  16. _, _, e1 := unix.Syscall(unix.SYS_IOCTL, uintptr(fd),
  17. uintptr(TCGETS), uintptr(unsafe.Pointer(state)))
  18. if e1 != 0 {
  19. err = e1
  20. }
  21. return
  22. }
  23. // int tcsetattr(int fd, int optional_actions, const struct termios *termios_p)
  24. func Setattr(fd int, action uint, state *Termios) (err error) {
  25. switch action {
  26. case TCSANOW:
  27. action = TCSETS
  28. case TCSADRAIN:
  29. action = TCSETSW
  30. case TCSAFLUSH:
  31. action = TCSETSF
  32. }
  33. _, _, e1 := unix.Syscall(unix.SYS_IOCTL, uintptr(fd),
  34. uintptr(action), uintptr(unsafe.Pointer(state)))
  35. if e1 != 0 {
  36. err = e1
  37. }
  38. return
  39. }
  40. // GetWinsize gets the winsize struct with the terminal size set by the kernel.
  41. func GetWinsize(fd int, ws *Winsize) (err error) {
  42. _, _, e1 := unix.Syscall(unix.SYS_IOCTL, uintptr(fd),
  43. uintptr(TIOCGWINSZ), uintptr(unsafe.Pointer(ws)))
  44. if e1 != 0 {
  45. err = e1
  46. }
  47. return
  48. }
  49. // Types
  50. //cgo const (TCSANOW, TCSADRAIN, TCSAFLUSH)
  51. //cgo const TIOCGWINSZ
  52. //cgo type struct_termios
  53. //cgo type struct_winsize
  54. //cgo// c_cc characters
  55. //cgo const (VINTR, VQUIT, VERASE, VKILL, VEOF, VTIME, VMIN, VSTART, VSTOP,
  56. // VSUSP, VEOL, VREPRINT, VDISCARD, VWERASE, VLNEXT, VEOL2)
  57. //cgo// c_iflag bits
  58. //cgo const (IGNBRK, BRKINT, IGNPAR, PARMRK, INPCK, ISTRIP, INLCR, IGNCR, ICRNL,
  59. // IXON, IXANY, IXOFF, IMAXBEL)
  60. //cgo// c_oflag bits
  61. //cgo const (OPOST, ONLCR, OCRNL, ONOCR, ONLRET, NL0, NL1, CR0, CR1, CR2, CR3,
  62. // TAB0, TAB1, TAB2, XTABS, BS0, BS1, FF0, FF1)
  63. //cgo// c_cflag bits
  64. //cgo const (B0, B50, B75, B110, B134, B150, B200, B300,
  65. // B600, B1200, B1800, B2400, B4800, B9600, B19200, B38400, EXTA, EXTB, CSIZE,
  66. // CS5, CS6, CS7, CS8, CSTOPB, CREAD, PARENB, PARODD, HUPCL, CLOCAL,
  67. // B57600, B115200, B230400, CRTSCTS)
  68. //cgo// c_lflag bits
  69. //cgo const (ISIG, ICANON, ECHO, ECHOE, ECHOK, ECHONL, NOFLSH, TOSTOP, ECHOCTL,
  70. // ECHOPRT, ECHOKE, FLUSHO, PENDIN, IEXTEN, EXTPROC)
  71. /*
  72. == FreeBSD has not:
  73. // c_cc characters
  74. VSWTC
  75. // c_iflag bits
  76. (IUCLC, IUTF8)
  77. // c_oflag bits
  78. (OLCUC, OFILL, OFDEL, NLDLY, CRDLY, BSDLY, VTDLY, VT0, VT1, FFDLY)
  79. // c_cflag bits
  80. (CBAUD, CBAUDEX, BOTHER, B500000, B576000,
  81. B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000,
  82. B4000000, CIBAUD, CMSPAR, IBSHIFT)
  83. // c_lflag bits
  84. XCASE
  85. == NetBSD, besides, has not:
  86. // c_oflag bits
  87. (TABDLY, TAB3)
  88. == OpenBSD, besides, has not:
  89. // c_cflag bits
  90. (B460800, B921600)
  91. */