diff --git a/term.go b/term.go index d145465..2cb4c38 100644 --- a/term.go +++ b/term.go @@ -4,42 +4,13 @@ package term import ( + "errors" "io" "os" "syscall" - - "github.com/pkg/term/termios" ) -// Term represents an asynchronous communications port. -type Term struct { - name string - fd int - orig syscall.Termios // original state of the terminal, see Open and Restore -} - -// 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...) -} - -// SetOption takes one or more option function and applies them in order to Term. -func (t *Term) SetOption(options ...func(*Term) error) error { - for _, opt := range options { - if err := opt(t); err != nil { - return err - } - } - return nil -} +var errNotSupported = errors.New("not supported") // Read reads up to len(b) bytes from the terminal. It returns the number of // bytes read and an error, if any. EOF is signaled by a zero count with @@ -58,6 +29,16 @@ func (t *Term) Read(b []byte) (int, error) { return n, nil } +// SetOption takes one or more option function and applies them in order to Term. +func (t *Term) SetOption(options ...func(*Term) error) error { + for _, opt := range options { + if err := opt(t); err != nil { + return err + } + } + return nil +} + // Write writes len(b) bytes to the terminal. It returns the number of bytes // written and an error, if any. Write returns a non-nil error when n != // len(b). @@ -74,111 +55,3 @@ func (t *Term) Write(b []byte) (int, error) { } return n, nil } - -// Close closes the device and releases any associated resources. -func (t *Term) Close() error { - err := syscall.Close(t.fd) - t.fd = -1 - return err -} - -// SetCbreak sets cbreak mode. -func (t *Term) SetCbreak() error { - return t.SetOption(CBreakMode) -} - -// CBreakMode places the terminal into cbreak mode. -func CBreakMode(t *Term) error { - var a attr - if err := termios.Tcgetattr(uintptr(t.fd), (*syscall.Termios)(&a)); err != nil { - return err - } - termios.Cfmakecbreak((*syscall.Termios)(&a)) - return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, (*syscall.Termios)(&a)) -} - -// SetRaw sets raw mode. -func (t *Term) SetRaw() error { - return t.SetOption(RawMode) -} - -// 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)) -} - -// Speed sets the baud rate option for the terminal. -func Speed(baud int) func(*Term) error { - return func(t *Term) error { - return t.setSpeed(baud) - } -} - -// SetSpeed sets the receive and transmit baud rates. -func (t *Term) SetSpeed(baud int) error { - return t.SetOption(Speed(baud)) -} - -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)) -} - -// Flush flushes both data received but not read, and data written but not transmitted. -func (t *Term) Flush() error { - return termios.Tcflush(uintptr(t.fd), termios.TCIOFLUSH) -} - -// SendBreak sends a break signal. -func (t *Term) SendBreak() error { - return termios.Tcsendbreak(uintptr(t.fd), 0) -} - -// SetDTR sets the DTR (data terminal ready) signal. -func (t *Term) SetDTR(v bool) error { - bits := syscall.TIOCM_DTR - if v { - return termios.Tiocmbis(uintptr(t.fd), &bits) - } else { - return termios.Tiocmbic(uintptr(t.fd), &bits) - } -} - -// DTR returns the state of the DTR (data terminal ready) signal. -func (t *Term) DTR() (bool, error) { - var status int - err := termios.Tiocmget(uintptr(t.fd), &status) - return status&syscall.TIOCM_DTR == syscall.TIOCM_DTR, err -} - -// SetRTS sets the RTS (data terminal ready) signal. -func (t *Term) SetRTS(v bool) error { - bits := syscall.TIOCM_RTS - if v { - return termios.Tiocmbis(uintptr(t.fd), &bits) - } else { - return termios.Tiocmbic(uintptr(t.fd), &bits) - } -} - -// RTS returns the state of the RTS (data terminal ready) signal. -func (t *Term) RTS() (bool, error) { - var status int - err := termios.Tiocmget(uintptr(t.fd), &status) - return status&syscall.TIOCM_RTS == syscall.TIOCM_RTS, err -} - -// Restore restores the state of the terminal captured at the point that -// the terminal was originally opened. -func (t *Term) Restore() error { - return termios.Tcsetattr(uintptr(t.fd), termios.TCIOFLUSH, &t.orig) -} diff --git a/term_posix.go b/term_posix.go new file mode 100644 index 0000000..ff3fb18 --- /dev/null +++ b/term_posix.go @@ -0,0 +1,138 @@ +// +build !windows + +package term + +import ( + "os" + "syscall" + + "github.com/pkg/term/termios" +) + +// Term represents an asynchronous communications port. +type Term struct { + name string + fd int + orig syscall.Termios // original state of the terminal, see Open and Restore +} + +// 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...) +} + +// SetCbreak sets cbreak mode. +func (t *Term) SetCbreak() error { + return t.SetOption(CBreakMode) +} + +// CBreakMode places the terminal into cbreak mode. +func CBreakMode(t *Term) error { + var a attr + if err := termios.Tcgetattr(uintptr(t.fd), (*syscall.Termios)(&a)); err != nil { + return err + } + termios.Cfmakecbreak((*syscall.Termios)(&a)) + return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, (*syscall.Termios)(&a)) +} + +// SetRaw sets raw mode. +func (t *Term) SetRaw() error { + return t.SetOption(RawMode) +} + +// 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)) +} + +// Speed sets the baud rate option for the terminal. +func Speed(baud int) func(*Term) error { + return func(t *Term) error { + return t.setSpeed(baud) + } +} + +// SetSpeed sets the receive and transmit baud rates. +func (t *Term) SetSpeed(baud int) error { + return t.SetOption(Speed(baud)) +} + +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)) +} + +// Flush flushes both data received but not read, and data written but not transmitted. +func (t *Term) Flush() error { + return termios.Tcflush(uintptr(t.fd), termios.TCIOFLUSH) +} + +// SendBreak sends a break signal. +func (t *Term) SendBreak() error { + return termios.Tcsendbreak(uintptr(t.fd), 0) +} + +// SetDTR sets the DTR (data terminal ready) signal. +func (t *Term) SetDTR(v bool) error { + bits := syscall.TIOCM_DTR + if v { + return termios.Tiocmbis(uintptr(t.fd), &bits) + } else { + return termios.Tiocmbic(uintptr(t.fd), &bits) + } +} + +// DTR returns the state of the DTR (data terminal ready) signal. +func (t *Term) DTR() (bool, error) { + var status int + err := termios.Tiocmget(uintptr(t.fd), &status) + return status&syscall.TIOCM_DTR == syscall.TIOCM_DTR, err +} + +// SetRTS sets the RTS (data terminal ready) signal. +func (t *Term) SetRTS(v bool) error { + bits := syscall.TIOCM_RTS + if v { + return termios.Tiocmbis(uintptr(t.fd), &bits) + } else { + return termios.Tiocmbic(uintptr(t.fd), &bits) + } +} + +// RTS returns the state of the RTS (data terminal ready) signal. +func (t *Term) RTS() (bool, error) { + var status int + err := termios.Tiocmget(uintptr(t.fd), &status) + return status&syscall.TIOCM_RTS == syscall.TIOCM_RTS, err +} + +// Restore restores the state of the terminal captured at the point that +// the terminal was originally opened. +func (t *Term) Restore() error { + return termios.Tcsetattr(uintptr(t.fd), termios.TCIOFLUSH, &t.orig) +} + +// Close closes the device and releases any associated resources. +func (t *Term) Close() error { + err := syscall.Close(t.fd) + t.fd = -1 + return err +} diff --git a/term_test.go b/term_test.go index 9dc2b3f..e9518b9 100644 --- a/term_test.go +++ b/term_test.go @@ -5,6 +5,27 @@ import "flag" var dev = flag.String("device", "/dev/tty", "device to use") +// assert that Term implements the same method set across +// all supported platforms +var _ interface { + Available() (int, error) + Buffered() (int, error) + Close() error + DTR() (bool, error) + Flush() error + RTS() (bool, error) + Read(b []byte) (int, error) + Restore() error + SendBreak() error + SetCbreak() error + SetDTR(v bool) error + SetOption(options ...func(*Term) error) error + SetRTS(v bool) error + SetRaw() error + SetSpeed(baud int) error + Write(b []byte) (int, error) +} = new(Term) + func TestTermSetCbreak(t *testing.T) { tt := opendev(t) defer tt.Close() diff --git a/term_windows.go b/term_windows.go new file mode 100644 index 0000000..570bacd --- /dev/null +++ b/term_windows.go @@ -0,0 +1,172 @@ +package term + +import ( + "errors" + "io" + "os" + "syscall" + + "github.com/pkg/term/termios" +) + +type Term struct { +} + +var errNotSupported = errors.New("not supported") + +// Open opens an asynchronous communications port. +func Open(name string, options ...func(*Term) error) (*Term, error) { + return nil, errNotSupported +} + +// SetOption takes one or more option function and applies them in order to Term. +func (t *Term) SetOption(options ...func(*Term) error) error { + for _, opt := range options { + if err := opt(t); err != nil { + return err + } + } + return nil +} + +// Read reads up to len(b) bytes from the terminal. It returns the number of +// bytes read and an error, if any. EOF is signaled by a zero count with +// err set to io.EOF. +func (t *Term) Read(b []byte) (int, error) { + n, e := syscall.Read(t.fd, b) + if n < 0 { + n = 0 + } + if n == 0 && len(b) > 0 && e == nil { + return 0, io.EOF + } + if e != nil { + return n, &os.PathError{"read", t.name, e} + } + return n, nil +} + +// Write writes len(b) bytes to the terminal. It returns the number of bytes +// written and an error, if any. Write returns a non-nil error when n != +// len(b). +func (t *Term) Write(b []byte) (int, error) { + n, e := syscall.Write(t.fd, b) + if n < 0 { + n = 0 + } + if n != len(b) { + return n, io.ErrShortWrite + } + if e != nil { + return n, &os.PathError{"write", t.name, e} + } + return n, nil +} + +// Close closes the device and releases any associated resources. +func (t *Term) Close() error { + err := syscall.Close(t.fd) + t.fd = -1 + return err +} + +// SetCbreak sets cbreak mode. +func (t *Term) SetCbreak() error { + return t.SetOption(CBreakMode) +} + +// CBreakMode places the terminal into cbreak mode. +func CBreakMode(t *Term) error { + var a attr + if err := termios.Tcgetattr(uintptr(t.fd), (*syscall.Termios)(&a)); err != nil { + return err + } + termios.Cfmakecbreak((*syscall.Termios)(&a)) + return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, (*syscall.Termios)(&a)) +} + +// SetRaw sets raw mode. +func (t *Term) SetRaw() error { + return t.SetOption(RawMode) +} + +// 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)) +} + +// Speed sets the baud rate option for the terminal. +func Speed(baud int) func(*Term) error { + return func(t *Term) error { + return t.setSpeed(baud) + } +} + +// SetSpeed sets the receive and transmit baud rates. +func (t *Term) SetSpeed(baud int) error { + return t.SetOption(Speed(baud)) +} + +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)) +} + +// Flush flushes both data received but not read, and data written but not transmitted. +func (t *Term) Flush() error { + return termios.Tcflush(uintptr(t.fd), termios.TCIOFLUSH) +} + +// SendBreak sends a break signal. +func (t *Term) SendBreak() error { + return termios.Tcsendbreak(uintptr(t.fd), 0) +} + +// SetDTR sets the DTR (data terminal ready) signal. +func (t *Term) SetDTR(v bool) error { + bits := syscall.TIOCM_DTR + if v { + return termios.Tiocmbis(uintptr(t.fd), &bits) + } else { + return termios.Tiocmbic(uintptr(t.fd), &bits) + } +} + +// DTR returns the state of the DTR (data terminal ready) signal. +func (t *Term) DTR() (bool, error) { + var status int + err := termios.Tiocmget(uintptr(t.fd), &status) + return status&syscall.TIOCM_DTR == syscall.TIOCM_DTR, err +} + +// SetRTS sets the RTS (data terminal ready) signal. +func (t *Term) SetRTS(v bool) error { + bits := syscall.TIOCM_RTS + if v { + return termios.Tiocmbis(uintptr(t.fd), &bits) + } else { + return termios.Tiocmbic(uintptr(t.fd), &bits) + } +} + +// RTS returns the state of the RTS (data terminal ready) signal. +func (t *Term) RTS() (bool, error) { + var status int + err := termios.Tiocmget(uintptr(t.fd), &status) + return status&syscall.TIOCM_RTS == syscall.TIOCM_RTS, err +} + +// Restore restores the state of the terminal captured at the point that +// the terminal was originally opened. +func (t *Term) Restore() error { + return termios.Tcsetattr(uintptr(t.fd), termios.TCIOFLUSH, &t.orig) +}