Skip to content

Commit f5c789d

Browse files
committed
all: apply golang.org/wiki/TargetSpecific
Also removed the obsolete appengine tag, switched _unsupported.go to negative tags, so that out-of-tree GOOSes compile out of the box, reduced use of build tags to reduce confusion, and renamed files that extend _unix.go to _unix_GOOS.go. Updates #31044 Change-Id: Ifb6f14c99713bb6a9edff630f90e9beffff3ed02 Reviewed-on: https://go-review.googlesource.com/c/term/+/258002 Run-TryBot: Filippo Valsorda <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Andrea Barisani <[email protected]> Reviewed-by: Katie Hockman <[email protected]> Trust: Filippo Valsorda <[email protected]>
1 parent d7a7210 commit f5c789d

13 files changed

+115
-107
lines changed

term.go

+38
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,45 @@
1414
// defer terminal.Restore(0, oldState)
1515
package term
1616

17+
// State contains the state of a terminal.
18+
type State struct {
19+
state
20+
}
21+
1722
// IsTerminal returns whether the given file descriptor is a terminal.
1823
func IsTerminal(fd int) bool {
1924
return isTerminal(fd)
2025
}
26+
27+
// MakeRaw puts the terminal connected to the given file descriptor into raw
28+
// mode and returns the previous state of the terminal so that it can be
29+
// restored.
30+
func MakeRaw(fd int) (*State, error) {
31+
return makeRaw(fd)
32+
}
33+
34+
// GetState returns the current state of a terminal which may be useful to
35+
// restore the terminal after a signal.
36+
func GetState(fd int) (*State, error) {
37+
return getState(fd)
38+
}
39+
40+
// Restore restores the terminal connected to the given file descriptor to a
41+
// previous state.
42+
func Restore(fd int, oldState *State) error {
43+
return restore(fd, oldState)
44+
}
45+
46+
// GetSize returns the visible dimensions of the given terminal.
47+
//
48+
// These dimensions don't include any scrollback buffer height.
49+
func GetSize(fd int) (width, height int, err error) {
50+
return getSize(fd)
51+
}
52+
53+
// ReadPassword reads a line of input from a terminal without local echo. This
54+
// is commonly used for inputting passwords and other sensitive data. The slice
55+
// returned does not include the \n.
56+
func ReadPassword(fd int) ([]byte, error) {
57+
return readPassword(fd)
58+
}

term_linux_test.go

-24
This file was deleted.

term_plan9.go

+6-17
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"golang.org/x/sys/plan9"
1212
)
1313

14-
type State struct{}
14+
type state struct{}
1515

1616
func isTerminal(fd int) bool {
1717
path, err := plan9.Fd2path(fd)
@@ -21,33 +21,22 @@ func isTerminal(fd int) bool {
2121
return path == "/dev/cons" || path == "/mnt/term/dev/cons"
2222
}
2323

24-
// MakeRaw put the terminal connected to the given file descriptor into raw
25-
// mode and returns the previous state of the terminal so that it can be
26-
// restored.
27-
func MakeRaw(fd int) (*State, error) {
24+
func makeRaw(fd int) (*State, error) {
2825
return nil, fmt.Errorf("terminal: MakeRaw not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
2926
}
3027

31-
// GetState returns the current state of a terminal which may be useful to
32-
// restore the terminal after a signal.
33-
func GetState(fd int) (*State, error) {
28+
func getState(fd int) (*State, error) {
3429
return nil, fmt.Errorf("terminal: GetState not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
3530
}
3631

37-
// Restore restores the terminal connected to the given file descriptor to a
38-
// previous state.
39-
func Restore(fd int, state *State) error {
32+
func restore(fd int, state *State) error {
4033
return fmt.Errorf("terminal: Restore not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
4134
}
4235

43-
// GetSize returns the dimensions of the given terminal.
44-
func GetSize(fd int) (width, height int, err error) {
36+
func getSize(fd int) (width, height int, err error) {
4537
return 0, 0, fmt.Errorf("terminal: GetSize not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
4638
}
4739

48-
// ReadPassword reads a line of input from a terminal without local echo. This
49-
// is commonly used for inputting passwords and other sensitive data. The slice
50-
// returned does not include the \n.
51-
func ReadPassword(fd int) ([]byte, error) {
40+
func readPassword(fd int) ([]byte, error) {
5241
return nil, fmt.Errorf("terminal: ReadPassword not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
5342
}

term_solaris.go

+9-20
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
// State contains the state of a terminal.
15-
type State struct {
15+
type state struct {
1616
termios unix.Termios
1717
}
1818

@@ -21,10 +21,7 @@ func isTerminal(fd int) bool {
2121
return err == nil
2222
}
2323

24-
// ReadPassword reads a line of input from a terminal without local echo. This
25-
// is commonly used for inputting passwords and other sensitive data. The slice
26-
// returned does not include the \n.
27-
func ReadPassword(fd int) ([]byte, error) {
24+
func readPassword(fd int) ([]byte, error) {
2825
// see also: http://src.illumos.org/source/xref/illumos-gate/usr/src/lib/libast/common/uwin/getpass.c
2926
val, err := unix.IoctlGetTermios(fd, unix.TCGETS)
3027
if err != nil {
@@ -68,17 +65,14 @@ func ReadPassword(fd int) ([]byte, error) {
6865
return ret, nil
6966
}
7067

71-
// MakeRaw puts the terminal connected to the given file descriptor into raw
72-
// mode and returns the previous state of the terminal so that it can be
73-
// restored.
74-
// see http://cr.illumos.org/~webrev/andy_js/1060/
75-
func MakeRaw(fd int) (*State, error) {
68+
func makeRaw(fd int) (*State, error) {
69+
// see http://cr.illumos.org/~webrev/andy_js/1060/
7670
termios, err := unix.IoctlGetTermios(fd, unix.TCGETS)
7771
if err != nil {
7872
return nil, err
7973
}
8074

81-
oldState := State{termios: *termios}
75+
oldState := State{state{termios: *termios}}
8276

8377
termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON
8478
termios.Oflag &^= unix.OPOST
@@ -95,25 +89,20 @@ func MakeRaw(fd int) (*State, error) {
9589
return &oldState, nil
9690
}
9791

98-
// Restore restores the terminal connected to the given file descriptor to a
99-
// previous state.
100-
func Restore(fd int, oldState *State) error {
92+
func restore(fd int, oldState *State) error {
10193
return unix.IoctlSetTermios(fd, unix.TCSETS, &oldState.termios)
10294
}
10395

104-
// GetState returns the current state of a terminal which may be useful to
105-
// restore the terminal after a signal.
106-
func GetState(fd int) (*State, error) {
96+
func getState(fd int) (*State, error) {
10797
termios, err := unix.IoctlGetTermios(fd, unix.TCGETS)
10898
if err != nil {
10999
return nil, err
110100
}
111101

112-
return &State{termios: *termios}, nil
102+
return &State{state{termios: *termios}}, nil
113103
}
114104

115-
// GetSize returns the dimensions of the given terminal.
116-
func GetSize(fd int) (width, height int, err error) {
105+
func getSize(fd int) (width, height int, err error) {
117106
ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
118107
if err != nil {
119108
return 0, 0, err

term_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package term_test
77
import (
88
"io/ioutil"
99
"os"
10+
"runtime"
1011
"testing"
1112

1213
"golang.org/x/term"
@@ -24,3 +25,18 @@ func TestIsTerminalTempFile(t *testing.T) {
2425
t.Fatalf("IsTerminal unexpectedly returned true for temporary file %s", file.Name())
2526
}
2627
}
28+
29+
func TestIsTerminalTerm(t *testing.T) {
30+
if runtime.GOOS != "linux" {
31+
t.Skipf("unknown terminal path for GOOS %v", runtime.GOOS)
32+
}
33+
file, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0)
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
defer file.Close()
38+
39+
if !term.IsTerminal(int(file.Fd())) {
40+
t.Fatalf("IsTerminal unexpectedly returned false for terminal file %s", file.Name())
41+
}
42+
}

term_unix.go

+9-21
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build aix darwin dragonfly freebsd linux,!appengine netbsd openbsd zos
5+
// +build aix darwin dragonfly freebsd linux netbsd openbsd zos
66

77
package term
88

99
import (
1010
"golang.org/x/sys/unix"
1111
)
1212

13-
// State contains the state of a terminal.
14-
type State struct {
13+
type state struct {
1514
termios unix.Termios
1615
}
1716

@@ -20,16 +19,13 @@ func isTerminal(fd int) bool {
2019
return err == nil
2120
}
2221

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) {
22+
func makeRaw(fd int) (*State, error) {
2723
termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
2824
if err != nil {
2925
return nil, err
3026
}
3127

32-
oldState := State{termios: *termios}
28+
oldState := State{state{termios: *termios}}
3329

3430
// This attempts to replicate the behaviour documented for cfmakeraw in
3531
// the termios(3) manpage.
@@ -47,25 +43,20 @@ func MakeRaw(fd int) (*State, error) {
4743
return &oldState, nil
4844
}
4945

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) {
46+
func getState(fd int) (*State, error) {
5347
termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
5448
if err != nil {
5549
return nil, err
5650
}
5751

58-
return &State{termios: *termios}, nil
52+
return &State{state{termios: *termios}}, nil
5953
}
6054

61-
// Restore restores the terminal connected to the given file descriptor to a
62-
// previous state.
63-
func Restore(fd int, state *State) error {
55+
func restore(fd int, state *State) error {
6456
return unix.IoctlSetTermios(fd, ioctlWriteTermios, &state.termios)
6557
}
6658

67-
// GetSize returns the dimensions of the given terminal.
68-
func GetSize(fd int) (width, height int, err error) {
59+
func getSize(fd int) (width, height int, err error) {
6960
ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
7061
if err != nil {
7162
return -1, -1, err
@@ -80,10 +71,7 @@ func (r passwordReader) Read(buf []byte) (int, error) {
8071
return unix.Read(int(r), buf)
8172
}
8273

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) {
74+
func readPassword(fd int) ([]byte, error) {
8775
termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
8876
if err != nil {
8977
return nil, err
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

term_unsupported.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,37 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build js,wasm nacl
5+
// +build !aix,!darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!zos,!windows,!solaris,!plan9
66

77
package term
88

9+
import (
10+
"fmt"
11+
"runtime"
12+
)
13+
14+
type state struct{}
15+
916
func isTerminal(fd int) bool {
1017
return false
1118
}
19+
20+
func makeRaw(fd int) (*State, error) {
21+
return nil, fmt.Errorf("terminal: MakeRaw not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
22+
}
23+
24+
func getState(fd int) (*State, error) {
25+
return nil, fmt.Errorf("terminal: GetState not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
26+
}
27+
28+
func restore(fd int, state *State) error {
29+
return fmt.Errorf("terminal: Restore not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
30+
}
31+
32+
func getSize(fd int) (width, height int, err error) {
33+
return 0, 0, fmt.Errorf("terminal: GetSize not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
34+
}
35+
36+
func readPassword(fd int) ([]byte, error) {
37+
return nil, fmt.Errorf("terminal: ReadPassword not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
38+
}

0 commit comments

Comments
 (0)