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:
Kurtis Rader 2020-08-06 20:16:34 -07:00 committed by Qi Xiao
parent 8f410a24be
commit bea90271b0
6 changed files with 11 additions and 46 deletions

View File

@ -18,7 +18,7 @@ func setup(in, out *os.File) (func() error, error) {
// 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)
}

View File

@ -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()))
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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.IoctlSetInt(fd, unix.TIOCSPGRP, pid)
}

View File

@ -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.
@ -68,5 +65,5 @@ func (term *Termios) SetICRNL(v bool) {
// FlushInput discards data written to a file descriptor but not read.
func FlushInput(fd int) error {
return Ioctl(fd, flushIOCTL, uintptr(unix.TCIFLUSH))
return unix.IoctlSetInt(fd, flushIOCTL, unix.TCIFLUSH)
}