8 "golang.org/x/sys/unix"
11 func tcget(fd uintptr, p *unix.Termios) error {
12 return ioctl(fd, unix.TIOCGETA, uintptr(unsafe.Pointer(p)))
15 func tcset(fd uintptr, p *unix.Termios) error {
16 return ioctl(fd, unix.TIOCSETA, uintptr(unsafe.Pointer(p)))
19 func ioctl(fd, flag, data uintptr) error {
20 if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, flag, data); err != 0 {
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 {
30 return ioctl(f.Fd(), unix.TIOCPTYUNLK, uintptr(unsafe.Pointer(&u)))
33 // ptsname retrieves the name of the first available pts for the given master.
34 func ptsname(f *os.File) (string, error) {
36 if err := ioctl(f.Fd(), unix.TIOCPTYGNAME, uintptr(unsafe.Pointer(&n))); err != nil {
39 return fmt.Sprintf("/dev/pts/%d", n), nil
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 {
48 // Set -onlcr so we don't have to deal with \r.
49 termios.Oflag &^= unix.ONLCR
50 return tcset(f.Fd(), &termios)