Skip to content

Commit

Permalink
Merge pull request #45 from ichizok/fix/solaris
Browse files Browse the repository at this point in the history
Fix solaris support
  • Loading branch information
mattn authored Jan 20, 2022
2 parents 72ed86c + ba2a7ca commit 6774b91
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 40 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ require (
github.com/mattn/go-colorable v0.1.4
github.com/mattn/go-isatty v0.0.10
github.com/mattn/go-runewidth v0.0.7
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-runewidth v0.0.6 h1:V2iyH+aX9C5fsYCpK60U8BYIvmhqxuOL3JZcqc1NB7k=
github.com/mattn/go-runewidth v0.0.6/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e h1:N7DeIrjYszNmSW409R3frPPwglRwMkXSBzwVbkOjLLA=
Expand Down
7 changes: 4 additions & 3 deletions tty_bsd.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
// +build darwin dragonfly freebsd netbsd openbsd

package tty

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

const (
ioctlReadTermios = syscall.TIOCGETA
ioctlWriteTermios = syscall.TIOCSETA
ioctlReadTermios = unix.TIOCGETA
ioctlWriteTermios = unix.TIOCSETA
)
9 changes: 7 additions & 2 deletions tty_linux.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
//go:build linux
// +build linux

package tty

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

const (
ioctlReadTermios = 0x5401 // syscall.TCGETS
ioctlWriteTermios = 0x5402 // syscall.TCSETS
ioctlReadTermios = unix.TCGETS
ioctlWriteTermios = unix.TCSETS
)
13 changes: 13 additions & 0 deletions tty_solaris.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build solaris
// +build solaris

package tty

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

const (
ioctlReadTermios = unix.TCGETS
ioctlWriteTermios = unix.TCSETS
)
12 changes: 0 additions & 12 deletions tty_sys5.go

This file was deleted.

37 changes: 19 additions & 18 deletions tty_unix.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// +build !windows
// +build !plan9
//go:build !windows && !plan9
// +build !windows,!plan9

package tty

import (
"bufio"
"os"
"os/signal"
"syscall"
"unsafe"

"golang.org/x/sys/unix"
)
Expand All @@ -17,7 +15,7 @@ type TTY struct {
in *os.File
bin *bufio.Reader
out *os.File
termios syscall.Termios
termios unix.Termios
ss chan os.Signal
}

Expand All @@ -31,19 +29,23 @@ func open(path string) (*TTY, error) {
tty.in = in
tty.bin = bufio.NewReader(in)

out, err := os.OpenFile(path, syscall.O_WRONLY, 0)
out, err := os.OpenFile(path, unix.O_WRONLY, 0)
if err != nil {
return nil, err
}
tty.out = out

if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(tty.in.Fd()), ioctlReadTermios, uintptr(unsafe.Pointer(&tty.termios))); err != 0 {
termios, err := unix.IoctlGetTermios(int(tty.in.Fd()), ioctlReadTermios)
if err != nil {
return nil, err
}
newios := tty.termios
newios.Iflag &^= syscall.ISTRIP | syscall.INLCR | syscall.ICRNL | syscall.IGNCR | syscall.IXOFF
newios.Lflag &^= syscall.ECHO | syscall.ICANON /*| syscall.ISIG*/
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(tty.in.Fd()), ioctlWriteTermios, uintptr(unsafe.Pointer(&newios))); err != 0 {
tty.termios = *termios

termios.Iflag &^= unix.ISTRIP | unix.INLCR | unix.ICRNL | unix.IGNCR | unix.IXOFF
termios.Lflag &^= unix.ECHO | unix.ICANON /*| unix.ISIG*/
termios.Cc[unix.VMIN] = 1
termios.Cc[unix.VTIME] = 0
if err := unix.IoctlSetTermios(int(tty.in.Fd()), ioctlWriteTermios, termios); err != nil {
return nil, err
}

Expand All @@ -64,8 +66,7 @@ func (tty *TTY) readRune() (rune, error) {
func (tty *TTY) close() error {
signal.Stop(tty.ss)
close(tty.ss)
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(tty.in.Fd()), ioctlWriteTermios, uintptr(unsafe.Pointer(&tty.termios)))
return err
return unix.IoctlSetTermios(int(tty.in.Fd()), ioctlWriteTermios, &tty.termios)
}

func (tty *TTY) size() (int, int, error) {
Expand All @@ -74,11 +75,11 @@ func (tty *TTY) size() (int, int, error) {
}

func (tty *TTY) sizePixel() (int, int, int, int, error) {
var dim [4]uint16
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(tty.out.Fd()), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&dim))); err != 0 {
ws, err := unix.IoctlGetWinsize(int(tty.out.Fd()), unix.TIOCGWINSZ)
if err != nil {
return -1, -1, -1, -1, err
}
return int(dim[1]), int(dim[0]), int(dim[2]), int(dim[3]), nil
return int(ws.Row), int(ws.Col), int(ws.Xpixel), int(ws.Ypixel), nil
}

func (tty *TTY) input() *os.File {
Expand Down Expand Up @@ -116,13 +117,13 @@ func (tty *TTY) raw() (func() error, error) {
}

func (tty *TTY) sigwinch() <-chan WINSIZE {
signal.Notify(tty.ss, syscall.SIGWINCH)
signal.Notify(tty.ss, unix.SIGWINCH)

ws := make(chan WINSIZE)
go func() {
defer close(ws)
for sig := range tty.ss {
if sig != syscall.SIGWINCH {
if sig != unix.SIGWINCH {
continue
}

Expand Down
1 change: 1 addition & 0 deletions tty_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package tty
Expand Down

0 comments on commit 6774b91

Please sign in to comment.