From 719a43ca3530c424c3875d8c066f7ab09195dfdc Mon Sep 17 00:00:00 2001 From: Frank Groeneveld Date: Fri, 18 Mar 2016 14:53:19 +0100 Subject: [PATCH] Cli binary can now be build on OpenBSD Signed-off-by: Frank Groeneveld --- layer/layer_unix.go | 2 +- pkg/system/stat_openbsd.go | 15 ++++++++ pkg/system/stat_unsupported.go | 2 +- pkg/term/termios_openbsd.go | 69 ++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 pkg/system/stat_openbsd.go create mode 100644 pkg/term/termios_openbsd.go diff --git a/layer/layer_unix.go b/layer/layer_unix.go index 524b97e8d1..d77e2fc66e 100644 --- a/layer/layer_unix.go +++ b/layer/layer_unix.go @@ -1,4 +1,4 @@ -// +build linux freebsd darwin +// +build linux freebsd darwin openbsd package layer diff --git a/pkg/system/stat_openbsd.go b/pkg/system/stat_openbsd.go new file mode 100644 index 0000000000..3c3b71fb21 --- /dev/null +++ b/pkg/system/stat_openbsd.go @@ -0,0 +1,15 @@ +package system + +import ( + "syscall" +) + +// fromStatT creates a system.StatT type from a syscall.Stat_t type +func fromStatT(s *syscall.Stat_t) (*StatT, error) { + return &StatT{size: s.Size, + mode: uint32(s.Mode), + uid: s.Uid, + gid: s.Gid, + rdev: uint64(s.Rdev), + mtim: s.Mtim}, nil +} diff --git a/pkg/system/stat_unsupported.go b/pkg/system/stat_unsupported.go index c6075d4ff2..f53e9de4d1 100644 --- a/pkg/system/stat_unsupported.go +++ b/pkg/system/stat_unsupported.go @@ -1,4 +1,4 @@ -// +build !linux,!windows,!freebsd,!solaris +// +build !linux,!windows,!freebsd,!solaris,!openbsd package system diff --git a/pkg/term/termios_openbsd.go b/pkg/term/termios_openbsd.go new file mode 100644 index 0000000000..ed843ad69c --- /dev/null +++ b/pkg/term/termios_openbsd.go @@ -0,0 +1,69 @@ +package term + +import ( + "syscall" + "unsafe" +) + +const ( + getTermios = syscall.TIOCGETA + setTermios = syscall.TIOCSETA +) + +// Termios magic numbers, passthrough to the ones defined in syscall. +const ( + IGNBRK = syscall.IGNBRK + PARMRK = syscall.PARMRK + INLCR = syscall.INLCR + IGNCR = syscall.IGNCR + ECHONL = syscall.ECHONL + CSIZE = syscall.CSIZE + ICRNL = syscall.ICRNL + ISTRIP = syscall.ISTRIP + PARENB = syscall.PARENB + ECHO = syscall.ECHO + ICANON = syscall.ICANON + ISIG = syscall.ISIG + IXON = syscall.IXON + BRKINT = syscall.BRKINT + INPCK = syscall.INPCK + OPOST = syscall.OPOST + CS8 = syscall.CS8 + IEXTEN = syscall.IEXTEN +) + +// Termios is the Unix API for terminal I/O. +type Termios struct { + Iflag uint32 + Oflag uint32 + Cflag uint32 + Lflag uint32 + Cc [20]byte + Ispeed uint32 + Ospeed uint32 +} + +// MakeRaw put the terminal connected to the given file descriptor into raw +// mode and returns the previous state of the terminal so that it can be +// restored. +func MakeRaw(fd uintptr) (*State, error) { + var oldState State + if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(&oldState.termios))); err != 0 { + return nil, err + } + + newState := oldState.termios + newState.Iflag &^= (IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON) + newState.Oflag &^= OPOST + newState.Lflag &^= (ECHO | ECHONL | ICANON | ISIG | IEXTEN) + newState.Cflag &^= (CSIZE | PARENB) + newState.Cflag |= CS8 + newState.Cc[syscall.VMIN] = 1 + newState.Cc[syscall.VTIME] = 0 + + if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(setTermios), uintptr(unsafe.Pointer(&newState))); err != 0 { + return nil, err + } + + return &oldState, nil +}