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.
5
// +build darwin dragonfly freebsd linux,!appengine netbsd openbsd
7
// Package terminal provides support functions for dealing with terminals, as
8
// commonly found on UNIX systems.
10
// Putting a terminal into raw mode is the most common requirement:
12
// oldState, err := terminal.MakeRaw(0)
16
// defer terminal.Restore(0, oldState)
17
package terminal // import "golang.org/x/crypto/ssh/terminal"
25
// State contains the state of a terminal.
27
termios syscall.Termios
30
// IsTerminal returns true if the given file descriptor is a terminal.
31
func IsTerminal(fd int) bool {
32
var termios syscall.Termios
33
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
37
// MakeRaw put the terminal connected to the given file descriptor into raw
38
// mode and returns the previous state of the terminal so that it can be
40
func MakeRaw(fd int) (*State, error) {
42
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 {
46
newState := oldState.termios
47
newState.Iflag &^= syscall.ISTRIP | syscall.INLCR | syscall.ICRNL | syscall.IGNCR | syscall.IXON | syscall.IXOFF
48
newState.Lflag &^= syscall.ECHO | syscall.ICANON | syscall.ISIG
49
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 {
56
// GetState returns the current state of a terminal which may be useful to
57
// restore the terminal after a signal.
58
func GetState(fd int) (*State, error) {
60
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 {
67
// Restore restores the terminal connected to the given file descriptor to a
69
func Restore(fd int, state *State) error {
70
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&state.termios)), 0, 0, 0)
74
// GetSize returns the dimensions of the given terminal.
75
func GetSize(fd int) (width, height int, err error) {
76
var dimensions [4]uint16
78
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&dimensions)), 0, 0, 0); err != 0 {
81
return int(dimensions[1]), int(dimensions[0]), nil
84
// ReadPassword reads a line of input from a terminal without local echo. This
85
// is commonly used for inputting passwords and other sensitive data. The slice
86
// returned does not include the \n.
87
func ReadPassword(fd int) ([]byte, error) {
88
var oldState syscall.Termios
89
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState)), 0, 0, 0); err != 0 {
94
newState.Lflag &^= syscall.ECHO
95
newState.Lflag |= syscall.ICANON | syscall.ISIG
96
newState.Iflag |= syscall.ICRNL
97
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 {
102
syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&oldState)), 0, 0, 0)
108
n, err := syscall.Read(fd, buf[:])
118
if buf[n-1] == '\n' {
121
ret = append(ret, buf[:n]...)