...
Source file src/pkg/cmd/vendor/golang.org/x/crypto/ssh/terminal/util_solaris.go
1
2
3
4
5
6
7 package terminal
8
9 import (
10 "golang.org/x/sys/unix"
11 "io"
12 "syscall"
13 )
14
15
16 type State struct {
17 termios unix.Termios
18 }
19
20
21 func IsTerminal(fd int) bool {
22 _, err := unix.IoctlGetTermio(fd, unix.TCGETA)
23 return err == nil
24 }
25
26
27
28
29 func ReadPassword(fd int) ([]byte, error) {
30
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
74
75
76
77 func 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
101
102 func Restore(fd int, oldState *State) error {
103 return unix.IoctlSetTermios(fd, unix.TCSETS, &oldState.termios)
104 }
105
106
107
108 func 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
118 func 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
View as plain text