util.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 aix 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. "golang.org/x/sys/unix"
  18. )
  19. // State contains the state of a terminal.
  20. type State struct {
  21. termios unix.Termios
  22. }
  23. // IsTerminal returns whether the given file descriptor is a terminal.
  24. func IsTerminal(fd int) bool {
  25. _, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
  26. return err == nil
  27. }
  28. // MakeRaw put the terminal connected to the given file descriptor into raw
  29. // mode and returns the previous state of the terminal so that it can be
  30. // restored.
  31. func MakeRaw(fd int) (*State, error) {
  32. termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
  33. if err != nil {
  34. return nil, err
  35. }
  36. oldState := State{termios: *termios}
  37. // This attempts to replicate the behaviour documented for cfmakeraw in
  38. // the termios(3) manpage.
  39. termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON
  40. termios.Oflag &^= unix.OPOST
  41. termios.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN
  42. termios.Cflag &^= unix.CSIZE | unix.PARENB
  43. termios.Cflag |= unix.CS8
  44. termios.Cc[unix.VMIN] = 1
  45. termios.Cc[unix.VTIME] = 0
  46. if err := unix.IoctlSetTermios(fd, ioctlWriteTermios, termios); err != nil {
  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. termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return &State{termios: *termios}, 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. return unix.IoctlSetTermios(fd, ioctlWriteTermios, &state.termios)
  64. }
  65. // GetSize returns the dimensions of the given terminal.
  66. func GetSize(fd int) (width, height int, err error) {
  67. ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
  68. if err != nil {
  69. return -1, -1, err
  70. }
  71. return int(ws.Col), int(ws.Row), nil
  72. }
  73. // passwordReader is an io.Reader that reads from a specific file descriptor.
  74. type passwordReader int
  75. func (r passwordReader) Read(buf []byte) (int, error) {
  76. return unix.Read(int(r), buf)
  77. }
  78. // ReadPassword reads a line of input from a terminal without local echo. This
  79. // is commonly used for inputting passwords and other sensitive data. The slice
  80. // returned does not include the \n.
  81. func ReadPassword(fd int) ([]byte, error) {
  82. termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
  83. if err != nil {
  84. return nil, err
  85. }
  86. newState := *termios
  87. newState.Lflag &^= unix.ECHO
  88. newState.Lflag |= unix.ICANON | unix.ISIG
  89. newState.Iflag |= unix.ICRNL
  90. if err := unix.IoctlSetTermios(fd, ioctlWriteTermios, &newState); err != nil {
  91. return nil, err
  92. }
  93. defer unix.IoctlSetTermios(fd, ioctlWriteTermios, termios)
  94. return readPasswordLine(passwordReader(fd))
  95. }