From dcf5866e1613169e8b955d641a27e79b1bbbeb83 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Tue, 1 Jun 2021 18:59:24 -0700 Subject: [PATCH] Permit a different device node to be used as /dev/tty. --- tty_unix.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tty_unix.go b/tty_unix.go index 436d2d8..a48b2e6 100644 --- a/tty_unix.go +++ b/tty_unix.go @@ -37,6 +37,7 @@ type devTty struct { sig chan os.Signal cb func() stopQ chan struct{} + dev string wg sync.WaitGroup l sync.Mutex } @@ -67,7 +68,7 @@ func (tty *devTty) Start() error { // we will have up to two separate file handles open on /dev/tty. (Note that when // using stdin/stdout instead of /dev/tty this problem is not observed.) var err error - if tty.f, err = os.OpenFile("/dev/tty", os.O_RDWR, 0); err != nil { + if tty.f, err = os.OpenFile(tty.dev, os.O_RDWR, 0); err != nil { return err } tty.fd = int(tty.of.Fd()) @@ -144,12 +145,19 @@ func (tty *devTty) NotifyResize(cb func()) { tty.l.Unlock() } +// NewDevTty opens a /dev/tty based Tty. func NewDevTty() (Tty, error) { + return NewDevTtyFromDev("/dev/tty") +} + +// NewDevTtyFromDev opens a tty device given a path. This can be useful to bind to other nodes. +func NewDevTtyFromDev(dev string) (Tty, error) { tty := &devTty{ + dev: dev, sig: make(chan os.Signal), } var err error - if tty.of, err = os.OpenFile("/dev/tty", os.O_RDWR, 0); err != nil { + if tty.of, err = os.OpenFile(dev, os.O_RDWR, 0); err != nil { return nil, err } tty.fd = int(tty.of.Fd())