util.go 2.9 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 linux
  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
  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.Tcgetattr(fd, &termios)
  29. return err == nil
  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.Tcgetattr(fd, &oldState.termios); err != nil {
  37. return nil, err
  38. }
  39. newState := oldState.termios
  40. newState.Iflag &^= syscall.ISTRIP | syscall.INLCR | syscall.ICRNL | syscall.IGNCR | syscall.IXON | syscall.IXOFF
  41. newState.Lflag &^= syscall.ECHO | syscall.ICANON | syscall.ISIG
  42. if err := syscall.Tcsetattr(fd, syscall.TCSANOW, &newState); err != nil {
  43. return nil, err
  44. }
  45. return &oldState, nil
  46. }
  47. // Restore restores the terminal connected to the given file descriptor to a
  48. // previous state.
  49. func Restore(fd int, state *State) error {
  50. err := syscall.Tcsetattr(fd, syscall.TCSANOW, &state.termios)
  51. return err
  52. }
  53. //extern ioctl
  54. func ioctl(int, int, unsafe.Pointer) int
  55. // GetSize returns the dimensions of the given terminal.
  56. func GetSize(fd int) (width, height int, err error) {
  57. var dimensions [4]uint16
  58. if ioctl(fd, syscall.TIOCGWINSZ, unsafe.Pointer(&dimensions)) < 0 {
  59. return -1, -1, syscall.GetErrno()
  60. }
  61. return int(dimensions[1]), int(dimensions[0]), nil
  62. }
  63. // ReadPassword reads a line of input from a terminal without local echo. This
  64. // is commonly used for inputting passwords and other sensitive data. The slice
  65. // returned does not include the \n.
  66. func ReadPassword(fd int) ([]byte, error) {
  67. var oldState syscall.Termios
  68. if err := syscall.Tcgetattr(fd, &oldState); err != nil {
  69. return nil, err
  70. }
  71. newState := oldState
  72. newState.Lflag &^= syscall.ECHO
  73. if err := syscall.Tcsetattr(fd, syscall.TCSANOW, &newState); err != nil {
  74. return nil, err
  75. }
  76. defer func() {
  77. syscall.Tcsetattr(fd, syscall.TCSANOW, &oldState)
  78. }()
  79. var buf [16]byte
  80. var ret []byte
  81. for {
  82. n, err := syscall.Read(fd, buf[:])
  83. if err != nil {
  84. return nil, err
  85. }
  86. if n == 0 {
  87. if len(ret) == 0 {
  88. return nil, io.EOF
  89. }
  90. break
  91. }
  92. if buf[n-1] == '\n' {
  93. n--
  94. }
  95. ret = append(ret, buf[:n]...)
  96. if n < len(buf) {
  97. break
  98. }
  99. }
  100. return ret, nil
  101. }