util.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build darwin dragonfly freebsd linux,!appengine netbsd openbsd
  5. // Package terminal provides support functions for dealing with terminals, as
  6. // commonly found on UNIX systems.
  7. //
  8. // Putting a terminal into raw mode is the most common requirement:
  9. //
  10. // oldState, err := terminal.MakeRaw(0)
  11. // if err != nil {
  12. // panic(err)
  13. // }
  14. // defer terminal.Restore(0, oldState)
  15. package terminal // import "golang.org/x/crypto/ssh/terminal"
  16. import (
  17. "io"
  18. "syscall"
  19. "unsafe"
  20. )
  21. // State contains the state of a terminal.
  22. type State struct {
  23. termios syscall.Termios
  24. }
  25. // IsTerminal returns true if the given file descriptor is a terminal.
  26. func IsTerminal(fd int) bool {
  27. var termios syscall.Termios
  28. _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
  29. return err == 0
  30. }
  31. // MakeRaw put the terminal connected to the given file descriptor into raw
  32. // mode and returns the previous state of the terminal so that it can be
  33. // restored.
  34. func MakeRaw(fd int) (*State, error) {
  35. var oldState State
  36. if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 {
  37. return nil, err
  38. }
  39. newState := oldState.termios
  40. // This attempts to replicate the behaviour documented for cfmakeraw in
  41. // the termios(3) manpage.
  42. newState.Iflag &^= syscall.IGNBRK | syscall.BRKINT | syscall.PARMRK | syscall.ISTRIP | syscall.INLCR | syscall.IGNCR | syscall.ICRNL | syscall.IXON
  43. newState.Oflag &^= syscall.OPOST
  44. newState.Lflag &^= syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.ISIG | syscall.IEXTEN
  45. newState.Cflag &^= syscall.CSIZE | syscall.PARENB
  46. newState.Cflag |= syscall.CS8
  47. if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 {
  48. return nil, err
  49. }
  50. return &oldState, nil
  51. }
  52. // GetState returns the current state of a terminal which may be useful to
  53. // restore the terminal after a signal.
  54. func GetState(fd int) (*State, error) {
  55. var oldState State
  56. if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 {
  57. return nil, err
  58. }
  59. return &oldState, nil
  60. }
  61. // Restore restores the terminal connected to the given file descriptor to a
  62. // previous state.
  63. func Restore(fd int, state *State) error {
  64. if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&state.termios)), 0, 0, 0); err != 0 {
  65. return err
  66. }
  67. return nil
  68. }
  69. // GetSize returns the dimensions of the given terminal.
  70. func GetSize(fd int) (width, height int, err error) {
  71. var dimensions [4]uint16
  72. if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&dimensions)), 0, 0, 0); err != 0 {
  73. return -1, -1, err
  74. }
  75. return int(dimensions[1]), int(dimensions[0]), nil
  76. }
  77. // ReadPassword reads a line of input from a terminal without local echo. This
  78. // is commonly used for inputting passwords and other sensitive data. The slice
  79. // returned does not include the \n.
  80. func ReadPassword(fd int) ([]byte, error) {
  81. var oldState syscall.Termios
  82. if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState)), 0, 0, 0); err != 0 {
  83. return nil, err
  84. }
  85. newState := oldState
  86. newState.Lflag &^= syscall.ECHO
  87. newState.Lflag |= syscall.ICANON | syscall.ISIG
  88. newState.Iflag |= syscall.ICRNL
  89. if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 {
  90. return nil, err
  91. }
  92. defer func() {
  93. syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&oldState)), 0, 0, 0)
  94. }()
  95. var buf [16]byte
  96. var ret []byte
  97. for {
  98. n, err := syscall.Read(fd, buf[:])
  99. if err != nil {
  100. return nil, err
  101. }
  102. if n == 0 {
  103. if len(ret) == 0 {
  104. return nil, io.EOF
  105. }
  106. break
  107. }
  108. if buf[n-1] == '\n' {
  109. n--
  110. }
  111. ret = append(ret, buf[:n]...)
  112. if n < len(buf) {
  113. break
  114. }
  115. }
  116. return ret, nil
  117. }