tc_unix.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // +build darwin freebsd linux openbsd solaris
  2. /*
  3. Copyright The containerd Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package console
  15. import (
  16. "golang.org/x/sys/unix"
  17. )
  18. func tcget(fd uintptr, p *unix.Termios) error {
  19. termios, err := unix.IoctlGetTermios(int(fd), cmdTcGet)
  20. if err != nil {
  21. return err
  22. }
  23. *p = *termios
  24. return nil
  25. }
  26. func tcset(fd uintptr, p *unix.Termios) error {
  27. return unix.IoctlSetTermios(int(fd), cmdTcSet, p)
  28. }
  29. func tcgwinsz(fd uintptr) (WinSize, error) {
  30. var ws WinSize
  31. uws, err := unix.IoctlGetWinsize(int(fd), unix.TIOCGWINSZ)
  32. if err != nil {
  33. return ws, err
  34. }
  35. // Translate from unix.Winsize to console.WinSize
  36. ws.Height = uws.Row
  37. ws.Width = uws.Col
  38. ws.x = uws.Xpixel
  39. ws.y = uws.Ypixel
  40. return ws, nil
  41. }
  42. func tcswinsz(fd uintptr, ws WinSize) error {
  43. // Translate from console.WinSize to unix.Winsize
  44. var uws unix.Winsize
  45. uws.Row = ws.Height
  46. uws.Col = ws.Width
  47. uws.Xpixel = ws.x
  48. uws.Ypixel = ws.y
  49. return unix.IoctlSetWinsize(int(fd), unix.TIOCSWINSZ, &uws)
  50. }
  51. func setONLCR(fd uintptr, enable bool) error {
  52. var termios unix.Termios
  53. if err := tcget(fd, &termios); err != nil {
  54. return err
  55. }
  56. if enable {
  57. // Set +onlcr so we can act like a real terminal
  58. termios.Oflag |= unix.ONLCR
  59. } else {
  60. // Set -onlcr so we don't have to deal with \r.
  61. termios.Oflag &^= unix.ONLCR
  62. }
  63. return tcset(fd, &termios)
  64. }
  65. func cfmakeraw(t unix.Termios) unix.Termios {
  66. t.Iflag &^= (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON)
  67. t.Oflag &^= unix.OPOST
  68. t.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN)
  69. t.Cflag &^= (unix.CSIZE | unix.PARENB)
  70. t.Cflag &^= unix.CS8
  71. t.Cc[unix.VMIN] = 1
  72. t.Cc[unix.VTIME] = 0
  73. return t
  74. }