// Open opens an asynchronous communications port. func Open(name string, options ...func(*Term) error) (*Term, error) { fd, e := syscall.Open(name, syscall.O_NOCTTY|syscall.O_CLOEXEC|syscall.O_NDELAY|syscall.O_RDWR, 0666) if e != nil { return nil, &os.PathError{"open", name, e} } modules := [2]string{"ptem", "ldterm"} for _, mod := range modules { err := unix.IoctlSetInt(fd, C.I_PUSH, int(uintptr(unsafe.Pointer(syscall.StringBytePtr(mod))))) if err != nil { return nil, err } } t := Term{name: name, fd: fd} termios.Tcgetattr(uintptr(t.fd), &t.orig) if err := termios.Tcgetattr(uintptr(t.fd), &t.orig); err != nil { return nil, err } if err := t.SetOption(options...); err != nil { return nil, err } return &t, syscall.SetNonblock(t.fd, false) }
func getTerm() syscall.Termios { var term syscall.Termios if err := termios.Tcgetattr(uintptr(syscall.Stdin), &term); err != nil { panic(err) } return term }
func tcgetattr(fd uintptr) (syscall.Termios, error) { var t syscall.Termios if err := termios.Tcgetattr(fd, &t); err != nil { return t, fmt.Errorf("tcgetattr: %v", err) } return t, nil }
func (t *Term) setSpeed(baud int) error { var a attr if err := termios.Tcgetattr(uintptr(t.fd), (*syscall.Termios)(&a)); err != nil { return err } a.setSpeed(baud) return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, (*syscall.Termios)(&a)) }
// RawMode places the terminal into raw mode. func RawMode(t *Term) error { var a attr if err := termios.Tcgetattr(uintptr(t.fd), (*syscall.Termios)(&a)); err != nil { return err } termios.Cfmakeraw((*syscall.Termios)(&a)) return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, (*syscall.Termios)(&a)) }
func (t *Term) setReadTimeout(d time.Duration) error { var a attr if err := termios.Tcgetattr(uintptr(t.fd), (*syscall.Termios)(&a)); err != nil { return err } a.Cc[syscall.VMIN], a.Cc[syscall.VTIME] = timeoutVals(d) return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, (*syscall.Termios)(&a)) }
// Open opens an asynchronous communications port. func Open(name string, options ...func(*Term) error) (*Term, error) { fd, e := syscall.Open(name, syscall.O_NOCTTY|syscall.O_CLOEXEC|syscall.O_RDWR, 0666) if e != nil { return nil, &os.PathError{"open", name, e} } t := Term{name: name, fd: fd} if err := termios.Tcgetattr(uintptr(t.fd), &t.orig); err != nil { return nil, err } return &t, t.SetOption(options...) }
func SetRaw(tty *os.File) error { var attr syscall.Termios err := termios.Tcgetattr(uintptr(tty.Fd()), (*syscall.Termios)(&attr)) if err != nil { return err } termios.Cfmakeraw((*syscall.Termios)(&attr)) return termios.Tcsetattr(uintptr(tty.Fd()), termios.TCSANOW, (*syscall.Termios)(&attr)) }
// Open opens an asynchronous communications port. func Open(name string, options ...func(*Term) error) (*Term, error) { fd, e := syscall.Open(name, syscall.O_NOCTTY|syscall.O_CLOEXEC|syscall.O_NDELAY|syscall.O_RDWR, 0666) if e != nil { return nil, &os.PathError{"open", name, e} } t := Term{name: name, fd: fd} if err := termios.Tcgetattr(uintptr(t.fd), &t.orig); err != nil { time.Sleep(time.Second * 100) return nil, err } if err := t.SetOption(options...); err != nil { return nil, err } return &t, syscall.SetNonblock(t.fd, false) }
func (p *Pty) Copy(o *os.File) (err error) { var attr syscall.Termios if err = termios.Tcgetattr(o.Fd(), &attr); err != nil { return err } w, h, err := GetSize(o.Fd()) if err != nil { return err } if err = SetSize(p.tty.Fd(), w, h); err != nil { return err } err = termios.Tcsetattr(p.tty.Fd(), termios.TCSADRAIN, &attr) if err != nil { return err } return nil }
func (t *Term) setFlowControl(kind int) error { var a attr if err := termios.Tcgetattr(uintptr(t.fd), (*syscall.Termios)(&a)); err != nil { return err } switch kind { case NONE: a.Iflag &^= termios.IXON | termios.IXOFF | termios.IXANY a.Cflag &^= termios.CRTSCTS case XONXOFF: a.Cflag &^= termios.CRTSCTS a.Iflag |= termios.IXON | termios.IXOFF | termios.IXANY case HARDWARE: a.Iflag &^= termios.IXON | termios.IXOFF | termios.IXANY a.Cflag |= termios.CRTSCTS } return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, (*syscall.Termios)(&a)) }