|
2 | 2 | // Use of this source code is governed by a BSD-style |
3 | 3 | // license that can be found in the LICENSE file. |
4 | 4 |
|
5 | | -// +build aix darwin dragonfly freebsd linux netbsd openbsd |
| 5 | +// +build aix darwin dragonfly freebsd linux,!appengine netbsd openbsd zos |
6 | 6 |
|
7 | 7 | package term |
8 | 8 |
|
9 | 9 | import ( |
10 | 10 | "golang.org/x/sys/unix" |
11 | 11 | ) |
12 | 12 |
|
| 13 | +// State contains the state of a terminal. |
| 14 | +type State struct { |
| 15 | + termios unix.Termios |
| 16 | +} |
| 17 | + |
13 | 18 | func isTerminal(fd int) bool { |
14 | 19 | _, err := unix.IoctlGetTermios(fd, ioctlReadTermios) |
15 | 20 | return err == nil |
16 | 21 | } |
| 22 | + |
| 23 | +// MakeRaw put the terminal connected to the given file descriptor into raw |
| 24 | +// mode and returns the previous state of the terminal so that it can be |
| 25 | +// restored. |
| 26 | +func MakeRaw(fd int) (*State, error) { |
| 27 | + termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + |
| 32 | + oldState := State{termios: *termios} |
| 33 | + |
| 34 | + // This attempts to replicate the behaviour documented for cfmakeraw in |
| 35 | + // the termios(3) manpage. |
| 36 | + termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON |
| 37 | + termios.Oflag &^= unix.OPOST |
| 38 | + termios.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN |
| 39 | + termios.Cflag &^= unix.CSIZE | unix.PARENB |
| 40 | + termios.Cflag |= unix.CS8 |
| 41 | + termios.Cc[unix.VMIN] = 1 |
| 42 | + termios.Cc[unix.VTIME] = 0 |
| 43 | + if err := unix.IoctlSetTermios(fd, ioctlWriteTermios, termios); err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + return &oldState, nil |
| 48 | +} |
| 49 | + |
| 50 | +// GetState returns the current state of a terminal which may be useful to |
| 51 | +// restore the terminal after a signal. |
| 52 | +func GetState(fd int) (*State, error) { |
| 53 | + termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + return &State{termios: *termios}, nil |
| 59 | +} |
| 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 | + return unix.IoctlSetTermios(fd, ioctlWriteTermios, &state.termios) |
| 65 | +} |
| 66 | + |
| 67 | +// GetSize returns the dimensions of the given terminal. |
| 68 | +func GetSize(fd int) (width, height int, err error) { |
| 69 | + ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ) |
| 70 | + if err != nil { |
| 71 | + return -1, -1, err |
| 72 | + } |
| 73 | + return int(ws.Col), int(ws.Row), nil |
| 74 | +} |
| 75 | + |
| 76 | +// passwordReader is an io.Reader that reads from a specific file descriptor. |
| 77 | +type passwordReader int |
| 78 | + |
| 79 | +func (r passwordReader) Read(buf []byte) (int, error) { |
| 80 | + return unix.Read(int(r), buf) |
| 81 | +} |
| 82 | + |
| 83 | +// ReadPassword reads a line of input from a terminal without local echo. This |
| 84 | +// is commonly used for inputting passwords and other sensitive data. The slice |
| 85 | +// returned does not include the \n. |
| 86 | +func ReadPassword(fd int) ([]byte, error) { |
| 87 | + termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios) |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + |
| 92 | + newState := *termios |
| 93 | + newState.Lflag &^= unix.ECHO |
| 94 | + newState.Lflag |= unix.ICANON | unix.ISIG |
| 95 | + newState.Iflag |= unix.ICRNL |
| 96 | + if err := unix.IoctlSetTermios(fd, ioctlWriteTermios, &newState); err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + |
| 100 | + defer unix.IoctlSetTermios(fd, ioctlWriteTermios, termios) |
| 101 | + |
| 102 | + return readPasswordLine(passwordReader(fd)) |
| 103 | +} |
0 commit comments