1// Copyright 2015 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 solaris 6 7package terminal // import "golang.org/x/crypto/ssh/terminal" 8 9import ( 10 "golang.org/x/sys/unix" 11 "io" 12 "syscall" 13) 14 15// State contains the state of a terminal. 16type State struct { 17 termios unix.Termios 18} 19 20// IsTerminal returns whether the given file descriptor is a terminal. 21func IsTerminal(fd int) bool { 22 _, err := unix.IoctlGetTermio(fd, unix.TCGETA) 23 return err == nil 24} 25 26// ReadPassword reads a line of input from a terminal without local echo. This 27// is commonly used for inputting passwords and other sensitive data. The slice 28// returned does not include the \n. 29func ReadPassword(fd int) ([]byte, error) { 30 // see also: http://src.illumos.org/source/xref/illumos-gate/usr/src/lib/libast/common/uwin/getpass.c 31 val, err := unix.IoctlGetTermios(fd, unix.TCGETS) 32 if err != nil { 33 return nil, err 34 } 35 oldState := *val 36 37 newState := oldState 38 newState.Lflag &^= syscall.ECHO 39 newState.Lflag |= syscall.ICANON | syscall.ISIG 40 newState.Iflag |= syscall.ICRNL 41 err = unix.IoctlSetTermios(fd, unix.TCSETS, &newState) 42 if err != nil { 43 return nil, err 44 } 45 46 defer unix.IoctlSetTermios(fd, unix.TCSETS, &oldState) 47 48 var buf [16]byte 49 var ret []byte 50 for { 51 n, err := syscall.Read(fd, buf[:]) 52 if err != nil { 53 return nil, err 54 } 55 if n == 0 { 56 if len(ret) == 0 { 57 return nil, io.EOF 58 } 59 break 60 } 61 if buf[n-1] == '\n' { 62 n-- 63 } 64 ret = append(ret, buf[:n]...) 65 if n < len(buf) { 66 break 67 } 68 } 69 70 return ret, nil 71} 72 73// MakeRaw puts the terminal connected to the given file descriptor into raw 74// mode and returns the previous state of the terminal so that it can be 75// restored. 76// see http://cr.illumos.org/~webrev/andy_js/1060/ 77func MakeRaw(fd int) (*State, error) { 78 termios, err := unix.IoctlGetTermios(fd, unix.TCGETS) 79 if err != nil { 80 return nil, err 81 } 82 83 oldState := State{termios: *termios} 84 85 termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON 86 termios.Oflag &^= unix.OPOST 87 termios.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN 88 termios.Cflag &^= unix.CSIZE | unix.PARENB 89 termios.Cflag |= unix.CS8 90 termios.Cc[unix.VMIN] = 1 91 termios.Cc[unix.VTIME] = 0 92 93 if err := unix.IoctlSetTermios(fd, unix.TCSETS, termios); err != nil { 94 return nil, err 95 } 96 97 return &oldState, nil 98} 99 100// Restore restores the terminal connected to the given file descriptor to a 101// previous state. 102func Restore(fd int, oldState *State) error { 103 return unix.IoctlSetTermios(fd, unix.TCSETS, &oldState.termios) 104} 105 106// GetState returns the current state of a terminal which may be useful to 107// restore the terminal after a signal. 108func GetState(fd int) (*State, error) { 109 termios, err := unix.IoctlGetTermios(fd, unix.TCGETS) 110 if err != nil { 111 return nil, err 112 } 113 114 return &State{termios: *termios}, nil 115} 116 117// GetSize returns the dimensions of the given terminal. 118func GetSize(fd int) (width, height int, err error) { 119 ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ) 120 if err != nil { 121 return 0, 0, err 122 } 123 return int(ws.Col), int(ws.Row), nil 124} 125