2017-12-28 04:43:43 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"sync"
|
2018-01-01 04:31:45 +08:00
|
|
|
|
2018-02-15 17:14:05 +08:00
|
|
|
"github.com/elves/elvish/eval/vals"
|
2017-12-28 04:43:43 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
stdoutChanSize = 32
|
|
|
|
stderrChanSize = 32
|
|
|
|
)
|
|
|
|
|
2018-03-01 10:35:32 +08:00
|
|
|
type stdPorts struct {
|
|
|
|
ports [3]*Port
|
|
|
|
relayerWait *sync.WaitGroup
|
2017-12-28 04:43:43 +08:00
|
|
|
}
|
|
|
|
|
2018-03-01 10:35:32 +08:00
|
|
|
func newStdPorts(stdin, stdout, stderr *os.File, prefix string) stdPorts {
|
2018-01-30 01:39:41 +08:00
|
|
|
stdoutChan := make(chan interface{}, stdoutChanSize)
|
|
|
|
stderrChan := make(chan interface{}, stderrChanSize)
|
2017-12-28 04:43:43 +08:00
|
|
|
|
2018-03-01 10:35:32 +08:00
|
|
|
relayerWait := new(sync.WaitGroup)
|
2017-12-28 04:43:43 +08:00
|
|
|
relayerWait.Add(2)
|
2018-03-01 10:35:32 +08:00
|
|
|
go relayChanToFile(stdoutChan, stdout, prefix, relayerWait)
|
|
|
|
go relayChanToFile(stderrChan, stderr, prefix, relayerWait)
|
2017-12-28 04:43:43 +08:00
|
|
|
|
2018-03-01 10:35:32 +08:00
|
|
|
return stdPorts{
|
2017-12-28 04:43:43 +08:00
|
|
|
[3]*Port{
|
2017-12-28 08:34:03 +08:00
|
|
|
{File: stdin, Chan: ClosedChan},
|
|
|
|
{File: stdout, Chan: stdoutChan, CloseChan: true},
|
|
|
|
{File: stderr, Chan: stderrChan, CloseChan: true},
|
2017-12-28 04:43:43 +08:00
|
|
|
},
|
2018-03-01 10:35:32 +08:00
|
|
|
relayerWait,
|
2017-12-28 04:43:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 10:35:32 +08:00
|
|
|
func relayChanToFile(ch <-chan interface{}, file *os.File, prefix string, w *sync.WaitGroup) {
|
2017-12-28 04:43:43 +08:00
|
|
|
for v := range ch {
|
2018-03-01 10:35:32 +08:00
|
|
|
file.WriteString(prefix)
|
2018-02-15 17:14:05 +08:00
|
|
|
file.WriteString(vals.Repr(v, initIndent))
|
2017-12-28 04:43:43 +08:00
|
|
|
file.WriteString("\n")
|
2017-12-28 04:43:43 +08:00
|
|
|
}
|
|
|
|
w.Done()
|
|
|
|
}
|
|
|
|
|
2018-03-01 10:35:32 +08:00
|
|
|
func (ep *stdPorts) close() {
|
2017-12-28 04:43:43 +08:00
|
|
|
ep.ports[1].Close()
|
|
|
|
ep.ports[2].Close()
|
2018-03-01 10:35:32 +08:00
|
|
|
ep.relayerWait.Wait()
|
2017-12-28 04:43:43 +08:00
|
|
|
}
|