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 <filippo@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Andrea Barisani <lcars.net@gmail.com>
Reviewed-by: Katie Hockman <katie@golang.org>
Trust: Filippo Valsorda <filippo@golang.org>
This commit is contained in:
Filippo Valsorda
2020-09-29 19:32:40 +02:00
parent d7a72108b8
commit f5c789dd32
13 changed files with 115 additions and 107 deletions

38
term.go
View File

@@ -14,7 +14,45 @@
// defer terminal.Restore(0, oldState) // defer terminal.Restore(0, oldState)
package term package term
// State contains the state of a terminal.
type State struct {
state
}
// IsTerminal returns whether the given file descriptor is a terminal. // IsTerminal returns whether the given file descriptor is a terminal.
func IsTerminal(fd int) bool { func IsTerminal(fd int) bool {
return isTerminal(fd) return isTerminal(fd)
} }
// MakeRaw puts the terminal connected to the given file descriptor into raw
// mode and returns the previous state of the terminal so that it can be
// restored.
func MakeRaw(fd int) (*State, error) {
return makeRaw(fd)
}
// GetState returns the current state of a terminal which may be useful to
// restore the terminal after a signal.
func GetState(fd int) (*State, error) {
return getState(fd)
}
// Restore restores the terminal connected to the given file descriptor to a
// previous state.
func Restore(fd int, oldState *State) error {
return restore(fd, oldState)
}
// GetSize returns the visible dimensions of the given terminal.
//
// These dimensions don't include any scrollback buffer height.
func GetSize(fd int) (width, height int, err error) {
return getSize(fd)
}
// ReadPassword reads a line of input from a terminal without local echo. This
// is commonly used for inputting passwords and other sensitive data. The slice
// returned does not include the \n.
func ReadPassword(fd int) ([]byte, error) {
return readPassword(fd)
}

View File

@@ -1,24 +0,0 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package term_test
import (
"os"
"testing"
"golang.org/x/term"
)
func TestIsTerminalTerm(t *testing.T) {
file, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0)
if err != nil {
t.Fatal(err)
}
defer file.Close()
if !term.IsTerminal(int(file.Fd())) {
t.Fatalf("IsTerminal unexpectedly returned false for terminal file %s", file.Name())
}
}

View File

@@ -11,7 +11,7 @@ import (
"golang.org/x/sys/plan9" "golang.org/x/sys/plan9"
) )
type State struct{} type state struct{}
func isTerminal(fd int) bool { func isTerminal(fd int) bool {
path, err := plan9.Fd2path(fd) path, err := plan9.Fd2path(fd)
@@ -21,33 +21,22 @@ func isTerminal(fd int) bool {
return path == "/dev/cons" || path == "/mnt/term/dev/cons" return path == "/dev/cons" || path == "/mnt/term/dev/cons"
} }
// MakeRaw put the terminal connected to the given file descriptor into raw func makeRaw(fd int) (*State, error) {
// mode and returns the previous state of the terminal so that it can be
// restored.
func MakeRaw(fd int) (*State, error) {
return nil, fmt.Errorf("terminal: MakeRaw not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) return nil, fmt.Errorf("terminal: MakeRaw not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
} }
// GetState returns the current state of a terminal which may be useful to func getState(fd int) (*State, error) {
// restore the terminal after a signal.
func GetState(fd int) (*State, error) {
return nil, fmt.Errorf("terminal: GetState not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) return nil, fmt.Errorf("terminal: GetState not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
} }
// Restore restores the terminal connected to the given file descriptor to a func restore(fd int, state *State) error {
// previous state.
func Restore(fd int, state *State) error {
return fmt.Errorf("terminal: Restore not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) return fmt.Errorf("terminal: Restore not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
} }
// GetSize returns the dimensions of the given terminal. func getSize(fd int) (width, height int, err error) {
func GetSize(fd int) (width, height int, err error) {
return 0, 0, fmt.Errorf("terminal: GetSize not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) return 0, 0, fmt.Errorf("terminal: GetSize not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
} }
// ReadPassword reads a line of input from a terminal without local echo. This func readPassword(fd int) ([]byte, error) {
// is commonly used for inputting passwords and other sensitive data. The slice
// returned does not include the \n.
func ReadPassword(fd int) ([]byte, error) {
return nil, fmt.Errorf("terminal: ReadPassword not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) return nil, fmt.Errorf("terminal: ReadPassword not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
} }

View File

@@ -12,7 +12,7 @@ import (
) )
// State contains the state of a terminal. // State contains the state of a terminal.
type State struct { type state struct {
termios unix.Termios termios unix.Termios
} }
@@ -21,10 +21,7 @@ func isTerminal(fd int) bool {
return err == nil return err == nil
} }
// ReadPassword reads a line of input from a terminal without local echo. This func readPassword(fd int) ([]byte, error) {
// is commonly used for inputting passwords and other sensitive data. The slice
// returned does not include the \n.
func ReadPassword(fd int) ([]byte, error) {
// see also: http://src.illumos.org/source/xref/illumos-gate/usr/src/lib/libast/common/uwin/getpass.c // see also: http://src.illumos.org/source/xref/illumos-gate/usr/src/lib/libast/common/uwin/getpass.c
val, err := unix.IoctlGetTermios(fd, unix.TCGETS) val, err := unix.IoctlGetTermios(fd, unix.TCGETS)
if err != nil { if err != nil {
@@ -68,17 +65,14 @@ func ReadPassword(fd int) ([]byte, error) {
return ret, nil return ret, nil
} }
// MakeRaw puts the terminal connected to the given file descriptor into raw func makeRaw(fd int) (*State, error) {
// mode and returns the previous state of the terminal so that it can be // see http://cr.illumos.org/~webrev/andy_js/1060/
// restored.
// see http://cr.illumos.org/~webrev/andy_js/1060/
func MakeRaw(fd int) (*State, error) {
termios, err := unix.IoctlGetTermios(fd, unix.TCGETS) termios, err := unix.IoctlGetTermios(fd, unix.TCGETS)
if err != nil { if err != nil {
return nil, err return nil, err
} }
oldState := State{termios: *termios} oldState := State{state{termios: *termios}}
termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON
termios.Oflag &^= unix.OPOST termios.Oflag &^= unix.OPOST
@@ -95,25 +89,20 @@ func MakeRaw(fd int) (*State, error) {
return &oldState, nil return &oldState, nil
} }
// Restore restores the terminal connected to the given file descriptor to a func restore(fd int, oldState *State) error {
// previous state.
func Restore(fd int, oldState *State) error {
return unix.IoctlSetTermios(fd, unix.TCSETS, &oldState.termios) return unix.IoctlSetTermios(fd, unix.TCSETS, &oldState.termios)
} }
// GetState returns the current state of a terminal which may be useful to func getState(fd int) (*State, error) {
// restore the terminal after a signal.
func GetState(fd int) (*State, error) {
termios, err := unix.IoctlGetTermios(fd, unix.TCGETS) termios, err := unix.IoctlGetTermios(fd, unix.TCGETS)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &State{termios: *termios}, nil return &State{state{termios: *termios}}, nil
} }
// GetSize returns the dimensions of the given terminal. func getSize(fd int) (width, height int, err error) {
func GetSize(fd int) (width, height int, err error) {
ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ) ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
if err != nil { if err != nil {
return 0, 0, err return 0, 0, err

View File

@@ -7,6 +7,7 @@ package term_test
import ( import (
"io/ioutil" "io/ioutil"
"os" "os"
"runtime"
"testing" "testing"
"golang.org/x/term" "golang.org/x/term"
@@ -24,3 +25,18 @@ func TestIsTerminalTempFile(t *testing.T) {
t.Fatalf("IsTerminal unexpectedly returned true for temporary file %s", file.Name()) t.Fatalf("IsTerminal unexpectedly returned true for temporary file %s", file.Name())
} }
} }
func TestIsTerminalTerm(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skipf("unknown terminal path for GOOS %v", runtime.GOOS)
}
file, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0)
if err != nil {
t.Fatal(err)
}
defer file.Close()
if !term.IsTerminal(int(file.Fd())) {
t.Fatalf("IsTerminal unexpectedly returned false for terminal file %s", file.Name())
}
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build aix darwin dragonfly freebsd linux,!appengine netbsd openbsd zos // +build aix darwin dragonfly freebsd linux netbsd openbsd zos
package term package term
@@ -10,8 +10,7 @@ import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
// State contains the state of a terminal. type state struct {
type State struct {
termios unix.Termios termios unix.Termios
} }
@@ -20,16 +19,13 @@ func isTerminal(fd int) bool {
return err == nil return err == nil
} }
// MakeRaw put the terminal connected to the given file descriptor into raw func makeRaw(fd int) (*State, error) {
// mode and returns the previous state of the terminal so that it can be
// restored.
func MakeRaw(fd int) (*State, error) {
termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios) termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
if err != nil { if err != nil {
return nil, err return nil, err
} }
oldState := State{termios: *termios} oldState := State{state{termios: *termios}}
// This attempts to replicate the behaviour documented for cfmakeraw in // This attempts to replicate the behaviour documented for cfmakeraw in
// the termios(3) manpage. // the termios(3) manpage.
@@ -47,25 +43,20 @@ func MakeRaw(fd int) (*State, error) {
return &oldState, nil return &oldState, nil
} }
// GetState returns the current state of a terminal which may be useful to func getState(fd int) (*State, error) {
// restore the terminal after a signal.
func GetState(fd int) (*State, error) {
termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios) termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &State{termios: *termios}, nil return &State{state{termios: *termios}}, nil
} }
// Restore restores the terminal connected to the given file descriptor to a func restore(fd int, state *State) error {
// previous state.
func Restore(fd int, state *State) error {
return unix.IoctlSetTermios(fd, ioctlWriteTermios, &state.termios) return unix.IoctlSetTermios(fd, ioctlWriteTermios, &state.termios)
} }
// GetSize returns the dimensions of the given terminal. func getSize(fd int) (width, height int, err error) {
func GetSize(fd int) (width, height int, err error) {
ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ) ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
if err != nil { if err != nil {
return -1, -1, err return -1, -1, err
@@ -80,10 +71,7 @@ func (r passwordReader) Read(buf []byte) (int, error) {
return unix.Read(int(r), buf) return unix.Read(int(r), buf)
} }
// ReadPassword reads a line of input from a terminal without local echo. This func readPassword(fd int) ([]byte, error) {
// is commonly used for inputting passwords and other sensitive data. The slice
// returned does not include the \n.
func ReadPassword(fd int) ([]byte, error) {
termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios) termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -2,10 +2,37 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build js,wasm nacl // +build !aix,!darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!zos,!windows,!solaris,!plan9
package term package term
import (
"fmt"
"runtime"
)
type state struct{}
func isTerminal(fd int) bool { func isTerminal(fd int) bool {
return false return false
} }
func makeRaw(fd int) (*State, error) {
return nil, fmt.Errorf("terminal: MakeRaw not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
}
func getState(fd int) (*State, error) {
return nil, fmt.Errorf("terminal: GetState not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
}
func restore(fd int, state *State) error {
return fmt.Errorf("terminal: Restore not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
}
func getSize(fd int) (width, height int, err error) {
return 0, 0, fmt.Errorf("terminal: GetSize not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
}
func readPassword(fd int) ([]byte, error) {
return nil, fmt.Errorf("terminal: ReadPassword not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
}

View File

@@ -10,7 +10,7 @@ import (
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
) )
type State struct { type state struct {
mode uint32 mode uint32
} }
@@ -20,10 +20,7 @@ func isTerminal(fd int) bool {
return err == nil return err == nil
} }
// MakeRaw put the terminal connected to the given file descriptor into raw func makeRaw(fd int) (*State, error) {
// mode and returns the previous state of the terminal so that it can be
// restored.
func MakeRaw(fd int) (*State, error) {
var st uint32 var st uint32
if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil { if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
return nil, err return nil, err
@@ -32,29 +29,22 @@ func MakeRaw(fd int) (*State, error) {
if err := windows.SetConsoleMode(windows.Handle(fd), raw); err != nil { if err := windows.SetConsoleMode(windows.Handle(fd), raw); err != nil {
return nil, err return nil, err
} }
return &State{st}, nil return &State{state{st}}, nil
} }
// GetState returns the current state of a terminal which may be useful to func getState(fd int) (*State, error) {
// restore the terminal after a signal.
func GetState(fd int) (*State, error) {
var st uint32 var st uint32
if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil { if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
return nil, err return nil, err
} }
return &State{st}, nil return &State{state{st}}, nil
} }
// Restore restores the terminal connected to the given file descriptor to a func restore(fd int, state *State) error {
// previous state.
func Restore(fd int, state *State) error {
return windows.SetConsoleMode(windows.Handle(fd), state.mode) return windows.SetConsoleMode(windows.Handle(fd), state.mode)
} }
// GetSize returns the visible dimensions of the given terminal. func getSize(fd int) (width, height int, err error) {
//
// These dimensions don't include any scrollback buffer height.
func GetSize(fd int) (width, height int, err error) {
var info windows.ConsoleScreenBufferInfo var info windows.ConsoleScreenBufferInfo
if err := windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info); err != nil { if err := windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info); err != nil {
return 0, 0, err return 0, 0, err
@@ -62,10 +52,7 @@ func GetSize(fd int) (width, height int, err error) {
return int(info.Window.Right - info.Window.Left + 1), int(info.Window.Bottom - info.Window.Top + 1), nil return int(info.Window.Right - info.Window.Left + 1), int(info.Window.Bottom - info.Window.Top + 1), nil
} }
// ReadPassword reads a line of input from a terminal without local echo. This func readPassword(fd int) ([]byte, error) {
// is commonly used for inputting passwords and other sensitive data. The slice
// returned does not include the \n.
func ReadPassword(fd int) ([]byte, error) {
var st uint32 var st uint32
if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil { if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
return nil, err return nil, err

View File

@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build aix darwin dragonfly freebsd linux,!appengine netbsd openbsd windows plan9 solaris
package term package term
import ( import (
@@ -430,7 +428,7 @@ func TestOutputNewlines(t *testing.T) {
term := NewTerminal(buf, ">") term := NewTerminal(buf, ">")
term.Write([]byte("1\n2\n")) term.Write([]byte("1\n2\n"))
output := string(buf.Bytes()) output := buf.String()
const expected = "1\r\n2\r\n" const expected = "1\r\n2\r\n"
if output != expected { if output != expected {