Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / containerd / console / tc_darwin.go
1 package console
2
3 import (
4         "fmt"
5         "os"
6         "unsafe"
7
8         "golang.org/x/sys/unix"
9 )
10
11 func tcget(fd uintptr, p *unix.Termios) error {
12         return ioctl(fd, unix.TIOCGETA, uintptr(unsafe.Pointer(p)))
13 }
14
15 func tcset(fd uintptr, p *unix.Termios) error {
16         return ioctl(fd, unix.TIOCSETA, uintptr(unsafe.Pointer(p)))
17 }
18
19 func ioctl(fd, flag, data uintptr) error {
20         if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, flag, data); err != 0 {
21                 return err
22         }
23         return nil
24 }
25
26 // unlockpt unlocks the slave pseudoterminal device corresponding to the master pseudoterminal referred to by f.
27 // unlockpt should be called before opening the slave side of a pty.
28 func unlockpt(f *os.File) error {
29         var u int32
30         return ioctl(f.Fd(), unix.TIOCPTYUNLK, uintptr(unsafe.Pointer(&u)))
31 }
32
33 // ptsname retrieves the name of the first available pts for the given master.
34 func ptsname(f *os.File) (string, error) {
35         var n int32
36         if err := ioctl(f.Fd(), unix.TIOCPTYGNAME, uintptr(unsafe.Pointer(&n))); err != nil {
37                 return "", err
38         }
39         return fmt.Sprintf("/dev/pts/%d", n), nil
40 }
41
42 func saneTerminal(f *os.File) error {
43         // Go doesn't have a wrapper for any of the termios ioctls.
44         var termios unix.Termios
45         if err := tcget(f.Fd(), &termios); err != nil {
46                 return err
47         }
48         // Set -onlcr so we don't have to deal with \r.
49         termios.Oflag &^= unix.ONLCR
50         return tcset(f.Fd(), &termios)
51 }