commit | b07888f6df6ec126c120fdf6b20bb440db03c589 | [log] [tgz] |
---|---|---|
author | Keith Rarick <kr@xph.us> | Mon Dec 03 23:17:35 2012 -0800 |
committer | Keith Rarick <kr@xph.us> | Mon Dec 03 23:17:35 2012 -0800 |
tree | 45a05beabc7141f8d2ba396d5cb9c04c00d89ba8 | |
parent | 2a896e26c7dee68a78d7e8639a566403df585b2c [diff] |
fix race in sample code Don't close the pty directly; instead send an EOT to cause the terminal to indicate end-of-file in the slave device. Closing the pty caused io.Copy to return early. Fixes #7.
Pty is a Go package for using unix pseudo-terminals.
(Note, there is only a Linux implementation. I'd appreciate a patch for other systems!)
go get github.com/kr/pty
package main import ( "github.com/kr/pty" "io" "os" "os/exec" ) func main() { c := exec.Command("grep", "--color=auto", "bar") f, err := pty.Start(c) if err != nil { panic(err) } go func() { f.Write([]byte("foo\n")) f.Write([]byte("bar\n")) f.Write([]byte("baz\n")) f.Write([]byte{4}) // EOT }() io.Copy(os.Stdout, f) }