Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ require (
github.com/creack/pty v1.1.9
github.com/google/go-cmp v0.4.0
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a
gotest.tools/v3 v3.0.2
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a h1:i47hUS795cOydZI4AwJQCKXOr4BvxzvikwDoDtHhP2Y=
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
Expand Down
19 changes: 9 additions & 10 deletions tc.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
// +build !windows,!illumos,!solaris
// +build !windows

package term

import (
"syscall"
"unsafe"

"golang.org/x/sys/unix"
)

func tcget(fd uintptr, p *Termios) syscall.Errno {
_, _, err := unix.Syscall(unix.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(p)))
return err
func tcget(fd uintptr) (*Termios, error) {
p, err := unix.IoctlGetTermios(int(fd), getTermios)
if err != nil {
return nil, err
}
return (*Termios)(p), nil
}

func tcset(fd uintptr, p *Termios) syscall.Errno {
_, _, err := unix.Syscall(unix.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(p)))
return err
func tcset(fd uintptr, p *Termios) error {
return unix.IoctlSetTermios(int(fd), setTermios, (*unix.Termios)(p))
}
25 changes: 0 additions & 25 deletions tc_illumos.go

This file was deleted.

20 changes: 8 additions & 12 deletions term.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !windows,!illumos,!solaris
// +build !windows

// Package term provides structures and helper functions to work with
// terminal (state, sizes).
Expand Down Expand Up @@ -50,8 +50,8 @@ func GetFdInfo(in interface{}) (uintptr, bool) {

// IsTerminal returns true if the given file descriptor is a terminal.
func IsTerminal(fd uintptr) bool {
var termios Termios
return tcget(fd, &termios) == 0
_, err := tcget(fd)
return err == nil
}

// RestoreTerminal restores the terminal connected to the given file descriptor
Expand All @@ -60,20 +60,16 @@ func RestoreTerminal(fd uintptr, state *State) error {
if state == nil {
return ErrInvalidState
}
if err := tcset(fd, &state.termios); err != 0 {
return err
}
return nil
return tcset(fd, &state.termios)
}

// SaveState saves the state of the terminal connected to the given file descriptor.
func SaveState(fd uintptr) (*State, error) {
var oldState State
if err := tcget(fd, &oldState.termios); err != 0 {
termios, err := tcget(fd)
if err != nil {
return nil, err
}

return &oldState, nil
return &State{termios: *termios}, nil
}

// DisableEcho applies the specified state to the terminal connected to the file
Expand All @@ -82,7 +78,7 @@ func DisableEcho(fd uintptr, state *State) error {
newState := state.termios
newState.Lflag &^= unix.ECHO

if err := tcset(fd, &newState); err != 0 {
if err := tcset(fd, &newState); err != nil {
return err
}
handleInterrupt(fd, state)
Expand Down
124 changes: 0 additions & 124 deletions term_illumos.go

This file was deleted.

13 changes: 5 additions & 8 deletions termios_linux.go → termios.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
// +build !windows

package term

import (
"golang.org/x/sys/unix"
)

const (
getTermios = unix.TCGETS
setTermios = unix.TCSETS
)

// Termios is the Unix API for terminal I/O.
type Termios unix.Termios

// MakeRaw put the terminal connected to the given file descriptor into raw
// 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 uintptr) (*State, error) {
termios, err := unix.IoctlGetTermios(int(fd), getTermios)
termios, err := tcget(fd)
if err != nil {
return nil, err
}
Expand All @@ -32,7 +29,7 @@ func MakeRaw(fd uintptr) (*State, error) {
termios.Cc[unix.VMIN] = 1
termios.Cc[unix.VTIME] = 0

if err := unix.IoctlSetTermios(int(fd), setTermios, termios); err != nil {
if err := tcset(fd, termios); err != nil {
return nil, err
}
return &oldState, nil
Expand Down
30 changes: 0 additions & 30 deletions termios_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,10 @@
package term

import (
"unsafe"

"golang.org/x/sys/unix"
)

const (
getTermios = unix.TIOCGETA
setTermios = unix.TIOCSETA
)

// Termios is the Unix API for terminal I/O.
type Termios unix.Termios

// MakeRaw put 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 uintptr) (*State, error) {
var oldState State
if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
return nil, err
}

newState := oldState.termios
newState.Iflag &^= (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON)
newState.Oflag &^= unix.OPOST
newState.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN)
newState.Cflag &^= (unix.CSIZE | unix.PARENB)
newState.Cflag |= unix.CS8
newState.Cc[unix.VMIN] = 1
newState.Cc[unix.VTIME] = 0

if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 {
return nil, err
}

return &oldState, nil
}
41 changes: 0 additions & 41 deletions termios_illumos.go

This file was deleted.

12 changes: 12 additions & 0 deletions termios_nonbsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//+build !darwin,!freebsd,!netbsd,!openbsd,!windows

package term

import (
"golang.org/x/sys/unix"
)

const (
getTermios = unix.TCGETS
setTermios = unix.TCSETS
)