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
5// +build darwin dragonfly freebsd linux,!appengine netbsd openbsd
6
7// Package terminal provides support functions for dealing with terminals, as
8// commonly found on UNIX systems.
9//
10// Putting a terminal into raw mode is the most common requirement:
11//
12// 	oldState, err := terminal.MakeRaw(0)
13// 	if err != nil {
14// 	        panic(err)
15// 	}
16// 	defer terminal.Restore(0, oldState)
17package terminal // import "golang.org/x/crypto/ssh/terminal"
18
19import (
20	"syscall"
21	"unsafe"
22
23	"golang.org/x/sys/unix"
24)
25
26// State contains the state of a terminal.
27type State struct {
28	termios syscall.Termios
29}
30
31// IsTerminal returns true if the given file descriptor is a terminal.
32func IsTerminal(fd int) bool {
33	var termios syscall.Termios
34	_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0)
35	return err == 0
36}
37
38// MakeRaw put the terminal connected to the given file descriptor into raw
39// mode and returns the previous state of the terminal so that it can be
40// restored.
41func MakeRaw(fd int) (*State, error) {
42	var oldState State
43	if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 {
44		return nil, err
45	}
46
47	newState := oldState.termios
48	// This attempts to replicate the behaviour documented for cfmakeraw in
49	// the termios(3) manpage.
50	newState.Iflag &^= syscall.IGNBRK | syscall.BRKINT | syscall.PARMRK | syscall.ISTRIP | syscall.INLCR | syscall.IGNCR | syscall.ICRNL | syscall.IXON
51	newState.Oflag &^= syscall.OPOST
52	newState.Lflag &^= syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.ISIG | syscall.IEXTEN
53	newState.Cflag &^= syscall.CSIZE | syscall.PARENB
54	newState.Cflag |= syscall.CS8
55	newState.Cc[unix.VMIN] = 1
56	newState.Cc[unix.VTIME] = 0
57	if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 {
58		return nil, err
59	}
60
61	return &oldState, nil
62}
63
64// GetState returns the current state of a terminal which may be useful to
65// restore the terminal after a signal.
66func GetState(fd int) (*State, error) {
67	var oldState State
68	if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 {
69		return nil, err
70	}
71
72	return &oldState, nil
73}
74
75// Restore restores the terminal connected to the given file descriptor to a
76// previous state.
77func Restore(fd int, state *State) error {
78	if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&state.termios)), 0, 0, 0); err != 0 {
79		return err
80	}
81	return nil
82}
83
84// GetSize returns the dimensions of the given terminal.
85func GetSize(fd int) (width, height int, err error) {
86	var dimensions [4]uint16
87
88	if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&dimensions)), 0, 0, 0); err != 0 {
89		return -1, -1, err
90	}
91	return int(dimensions[1]), int(dimensions[0]), nil
92}
93
94// passwordReader is an io.Reader that reads from a specific file descriptor.
95type passwordReader int
96
97func (r passwordReader) Read(buf []byte) (int, error) {
98	return syscall.Read(int(r), buf)
99}
100
101// ReadPassword reads a line of input from a terminal without local echo.  This
102// is commonly used for inputting passwords and other sensitive data. The slice
103// returned does not include the \n.
104func ReadPassword(fd int) ([]byte, error) {
105	var oldState syscall.Termios
106	if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState)), 0, 0, 0); err != 0 {
107		return nil, err
108	}
109
110	newState := oldState
111	newState.Lflag &^= syscall.ECHO
112	newState.Lflag |= syscall.ICANON | syscall.ISIG
113	newState.Iflag |= syscall.ICRNL
114	if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 {
115		return nil, err
116	}
117
118	defer func() {
119		syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&oldState)), 0, 0, 0)
120	}()
121
122	return readPasswordLine(passwordReader(fd))
123}
124