mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-11-27 23:11:20 +08:00
Eliminate sys.Ioctl
I started by writing tests for sys.Ioctl but after I had 100% coverage of that function it occurred to me the function could be eliminated. Related #1062
This commit is contained in:
parent
37001a42f2
commit
eed744044c
2
go.mod
2
go.mod
|
@ -6,7 +6,7 @@ require (
|
|||
github.com/mattn/go-isatty v0.0.12
|
||||
github.com/xiaq/persistent v0.0.0-20200820214153-3175cfb92e14
|
||||
go.etcd.io/bbolt v1.3.5
|
||||
golang.org/x/sys v0.0.0-20200820212457-1fb795427249
|
||||
golang.org/x/sys v0.0.0-20200824131525-c12d262b63d8
|
||||
)
|
||||
|
||||
go 1.14
|
||||
|
|
2
go.sum
2
go.sum
|
@ -13,3 +13,5 @@ golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfru
|
|||
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200820212457-1fb795427249 h1:tKP05IMsVLZ4VeeCEFmrIUmxAAx6UD8IBdPtYlYNa8g=
|
||||
golang.org/x/sys v0.0.0-20200820212457-1fb795427249/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200824131525-c12d262b63d8 h1:AvbQYmiaaaza3cW3QXRyPo5kYgpFIzOAfeAAN7m3qQ4=
|
||||
golang.org/x/sys v0.0.0-20200824131525-c12d262b63d8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
|
|
@ -11,14 +11,12 @@ import (
|
|||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
const flushInputDuringSetup = false
|
||||
|
||||
func setup(in, out *os.File) (func() error, error) {
|
||||
// On Unix, use input file for changing termios. All fds pointing to the
|
||||
// same terminal are equivalent.
|
||||
|
||||
fd := int(in.Fd())
|
||||
term, err := sys.NewTermiosFromFd(fd)
|
||||
term, err := sys.TermiosForFd(fd)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't get terminal attribute: %s", err)
|
||||
}
|
||||
|
@ -40,14 +38,6 @@ func setup(in, out *os.File) (func() error, error) {
|
|||
return nil, fmt.Errorf("can't set up terminal attribute: %s", err)
|
||||
}
|
||||
|
||||
var errFlushInput error
|
||||
if flushInputDuringSetup {
|
||||
err = sys.FlushInput(fd)
|
||||
if err != nil {
|
||||
errFlushInput = fmt.Errorf("can't flush input: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
var errSetupVT error
|
||||
err = setupVT(out)
|
||||
if err != nil {
|
||||
|
@ -58,7 +48,7 @@ func setup(in, out *os.File) (func() error, error) {
|
|||
return util.Errors(savedTermios.ApplyToFd(fd), restoreVT(out))
|
||||
}
|
||||
|
||||
return restore, util.Errors(errFlushInput, errSetupVT)
|
||||
return restore, errSetupVT
|
||||
}
|
||||
|
||||
func setupGlobal() func() {
|
||||
|
|
|
@ -22,5 +22,5 @@ func TestSetupTerminal(t *testing.T) {
|
|||
}
|
||||
// TODO(xiaq): Test whether the interesting flags in the termios were indeed
|
||||
// set.
|
||||
// termios, err := sys.NewTermiosFromFd(int(tty.Fd()))
|
||||
// termios, err := sys.TermiosForFd(int(tty.Fd()))
|
||||
}
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
// +build !windows,!plan9
|
||||
|
||||
package sys
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Ioctl wraps the ioctl syscall.
|
||||
func Ioctl(fd int, req uintptr, arg uintptr) error {
|
||||
_, _, e := unix.Syscall(
|
||||
unix.SYS_IOCTL, uintptr(fd), req, arg)
|
||||
if e != 0 {
|
||||
return os.NewSyscallError("ioctl", e)
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -4,12 +4,10 @@ package sys
|
|||
|
||||
import (
|
||||
"os"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// IsATTY returns true if the given file is a terminal.
|
||||
func IsATTY(file *os.File) bool {
|
||||
var term Termios
|
||||
err := Ioctl(int(file.Fd()), getAttrIOCTL, uintptr(unsafe.Pointer(&term)))
|
||||
_, err := TermiosForFd(int(file.Fd()))
|
||||
return err == nil
|
||||
}
|
||||
|
|
|
@ -3,20 +3,9 @@
|
|||
package sys
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func Tcgetpgrp(fd int) (int, error) {
|
||||
var pid int
|
||||
errno := Ioctl(fd, unix.TIOCGPGRP, uintptr(unsafe.Pointer(&pid)))
|
||||
if errno == nil {
|
||||
return pid, nil
|
||||
}
|
||||
return -1, errno
|
||||
}
|
||||
|
||||
func Tcsetpgrp(fd int, pid int) error {
|
||||
return Ioctl(fd, unix.TIOCSPGRP, uintptr(unsafe.Pointer(&pid)))
|
||||
return unix.IoctlSetPointerInt(fd, unix.TIOCSPGRP, pid)
|
||||
}
|
||||
|
|
|
@ -15,19 +15,16 @@ import (
|
|||
// Termios represents terminal attributes.
|
||||
type Termios unix.Termios
|
||||
|
||||
// NewTermiosFromFd extracts the terminal attribute of the given file
|
||||
// descriptor.
|
||||
func NewTermiosFromFd(fd int) (*Termios, error) {
|
||||
var term Termios
|
||||
if err := Ioctl(fd, getAttrIOCTL, uintptr(unsafe.Pointer(&term))); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &term, nil
|
||||
// TermiosForFd returns a pointer to a Termios structure if the file
|
||||
// descriptor is open on a terminal device.
|
||||
func TermiosForFd(fd int) (*Termios, error) {
|
||||
term, err := unix.IoctlGetTermios(fd, getAttrIOCTL)
|
||||
return (*Termios)(term), err
|
||||
}
|
||||
|
||||
// ApplyToFd applies term to the given file descriptor.
|
||||
func (term *Termios) ApplyToFd(fd int) error {
|
||||
return Ioctl(fd, setAttrNowIOCTL, uintptr(unsafe.Pointer(term)))
|
||||
return unix.IoctlSetTermios(fd, setAttrNowIOCTL, (*unix.Termios)(unsafe.Pointer(term)))
|
||||
}
|
||||
|
||||
// Copy returns a copy of term.
|
||||
|
@ -65,8 +62,3 @@ func (term *Termios) SetEcho(v bool) {
|
|||
func (term *Termios) SetICRNL(v bool) {
|
||||
setFlag(&term.Iflag, unix.ICRNL, v)
|
||||
}
|
||||
|
||||
// FlushInput discards data written to a file descriptor but not read.
|
||||
func FlushInput(fd int) error {
|
||||
return Ioctl(fd, flushIOCTL, uintptr(unix.TCIFLUSH))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user