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

60 lines
1.3 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)))
return ws, err
}
func SetWinsize(fd uintptr, ws *Winsize) error {
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(syscall.TIOCSWINSZ), uintptr(unsafe.Pointer(ws)))
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 SetRawTerminal(fd uintptr) (*State, error) {
oldState, err := MakeRaw(fd)
2013-05-14 18:37:35 -04:00
if err != nil {
return nil, err
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
_ = <-c
RestoreTerminal(fd, oldState)
2013-05-14 18:37:35 -04:00
os.Exit(0)
}()
return oldState, err
}