util.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "syscall"
  18. "unsafe"
  19. )
  20. // State contains the state of a terminal.
  21. type State struct {
  22. termios syscall.Termios
  23. }
  24. // IsTerminal returns true if the given file descriptor is a terminal.
  25. func IsTerminal(fd int) bool {
  26. var termios syscall.Termios
  27. _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
  28. return err == 0
  29. }
  30. // MakeRaw put the terminal connected to the given file descriptor into raw
  31. // mode and returns the previous state of the terminal so that it can be
  32. // restored.
  33. func MakeRaw(fd int) (*State, error) {
  34. var oldState State
  35. if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 {
  36. return nil, err
  37. }
  38. newState := oldState.termios
  39. // This attempts to replicate the behaviour documented for cfmakeraw in
  40. // the termios(3) manpage.
  41. newState.Iflag &^= syscall.IGNBRK | syscall.BRKINT | syscall.PARMRK | syscall.ISTRIP | syscall.INLCR | syscall.IGNCR | syscall.ICRNL | syscall.IXON
  42. newState.Oflag &^= syscall.OPOST
  43. newState.Lflag &^= syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.ISIG | syscall.IEXTEN
  44. newState.Cflag &^= syscall.CSIZE | syscall.PARENB
  45. newState.Cflag |= syscall.CS8
  46. if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 {
  47. return nil, err
  48. }
  49. return &oldState, nil
  50. }
  51. // GetState returns the current state of a terminal which may be useful to
  52. // restore the terminal after a signal.
  53. func GetState(fd int) (*State, error) {
  54. var oldState State
  55. if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 {
  56. return nil, err
  57. }
  58. return &oldState, nil
  59. }
  60. // Restore restores the terminal connected to the given file descriptor to a
  61. // previous state.
  62. func Restore(fd int, state *State) error {
  63. if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&state.termios)), 0, 0, 0); err != 0 {
  64. return err
  65. }
  66. return nil
  67. }
  68. // GetSize returns the dimensions of the given terminal.
  69. func GetSize(fd int) (width, height int, err error) {
  70. var dimensions [4]uint16
  71. if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&dimensions)), 0, 0, 0); err != 0 {
  72. return -1, -1, err
  73. }
  74. return int(dimensions[1]), int(dimensions[0]), nil
  75. }
  76. // passwordReader is an io.Reader that reads from a specific file descriptor.
  77. type passwordReader int
  78. func (r passwordReader) Read(buf []byte) (int, error) {
  79. return syscall.Read(int(r), buf)
  80. }
  81. // ReadPassword reads a line of input from a terminal without local echo. This
  82. // is commonly used for inputting passwords and other sensitive data. The slice
  83. // returned does not include the \n.
  84. func ReadPassword(fd int) ([]byte, error) {
  85. var oldState syscall.Termios
  86. if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState)), 0, 0, 0); err != 0 {
  87. return nil, err
  88. }
  89. newState := oldState
  90. newState.Lflag &^= syscall.ECHO
  91. newState.Lflag |= syscall.ICANON | syscall.ISIG
  92. newState.Iflag |= syscall.ICRNL
  93. if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 {
  94. return nil, err
  95. }
  96. defer func() {
  97. syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&oldState)), 0, 0, 0)
  98. }()
  99. return readPasswordLine(passwordReader(fd))
  100. }