1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/term/term.go

93 lines
2.1 KiB
Go
Raw Normal View History

package term
import (
2013-05-14 18:37:35 -04:00
"os"
"os/signal"
"syscall"
"unsafe"
)
type State struct {
2013-02-26 20:26:46 -05:00
termios Termios
}
type Winsize struct {
Height uint16
Width uint16
x uint16
y uint16
}
func GetWinsize(fd uintptr) (*Winsize, error) {
ws := &Winsize{}
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(ws)))
2013-10-16 19:05:50 -04:00
// Skipp errno = 0
if err == 0 {
return ws, nil
}
return ws, err
}
func SetWinsize(fd uintptr, ws *Winsize) error {
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(syscall.TIOCSWINSZ), uintptr(unsafe.Pointer(ws)))
2013-10-16 19:05:50 -04:00
// Skipp errno = 0
if err == 0 {
return nil
}
return err
}
// IsTerminal returns true if the given file descriptor is a terminal.
2013-06-01 18:55:05 -04:00
func IsTerminal(fd uintptr) bool {
2013-02-26 20:26:46 -05:00
var termios Termios
2013-06-01 18:55:05 -04:00
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(&termios)))
2013-02-26 20:26:46 -05:00
return err == 0
}
// Restore restores the terminal connected to the given file descriptor to a
// previous state.
func RestoreTerminal(fd uintptr, state *State) error {
2013-06-01 18:55:05 -04:00
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(setTermios), uintptr(unsafe.Pointer(&state.termios)))
2013-02-26 20:26:46 -05:00
return err
}
2013-05-14 18:37:35 -04:00
func SaveState(fd uintptr) (*State, error) {
var oldState State
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
2013-05-14 18:37:35 -04:00
return nil, err
}
return &oldState, nil
}
2013-08-18 00:29:37 -04:00
func DisableEcho(fd uintptr, state *State) error {
newState := state.termios
newState.Lflag &^= syscall.ECHO
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 {
return err
}
2013-08-18 00:29:37 -04:00
handleInterrupt(fd, state)
return nil
}
2013-08-18 00:29:37 -04:00
func SetRawTerminal(fd uintptr) (*State, error) {
oldState, err := MakeRaw(fd)
if err != nil {
return nil, err
}
handleInterrupt(fd, oldState)
return oldState, err
}
func handleInterrupt(fd uintptr, state *State) {
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt)
2013-05-14 18:37:35 -04:00
go func() {
_ = <-sigchan
RestoreTerminal(fd, state)
2013-05-14 18:37:35 -04:00
os.Exit(0)
}()
}