2015-01-26 23:27:24 +08:00
|
|
|
package eval
|
|
|
|
|
2018-01-01 04:31:45 +08:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
)
|
2015-01-26 23:27:24 +08:00
|
|
|
|
2016-02-09 03:11:20 +08:00
|
|
|
// Port conveys data stream. It always consists of a byte band and a channel band.
|
|
|
|
type Port struct {
|
|
|
|
File *os.File
|
2018-01-30 01:39:41 +08:00
|
|
|
Chan chan interface{}
|
2016-02-09 03:11:20 +08:00
|
|
|
CloseFile bool
|
|
|
|
CloseChan bool
|
2015-01-26 23:27:24 +08:00
|
|
|
}
|
|
|
|
|
2016-02-09 08:40:36 +08:00
|
|
|
// Fork returns a copy of a Port with the Close* flags unset.
|
|
|
|
func (p *Port) Fork() *Port {
|
|
|
|
return &Port{p.File, p.Chan, false, false}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes a Port.
|
2016-02-09 03:27:05 +08:00
|
|
|
func (p *Port) Close() {
|
2016-01-23 01:05:15 +08:00
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
2016-02-09 03:11:20 +08:00
|
|
|
if p.CloseFile {
|
|
|
|
p.File.Close()
|
2016-01-23 01:05:15 +08:00
|
|
|
}
|
2016-02-09 03:11:20 +08:00
|
|
|
if p.CloseChan {
|
|
|
|
close(p.Chan)
|
2016-01-23 01:05:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 22:54:58 +08:00
|
|
|
var (
|
2016-10-13 23:44:28 +08:00
|
|
|
// ClosedChan is a closed channel, suitable for use as placeholder channel input.
|
2018-01-30 01:39:41 +08:00
|
|
|
ClosedChan = make(chan interface{})
|
2016-10-13 23:44:28 +08:00
|
|
|
// BlackholeChan is channel writes onto which disappear, suitable for use as
|
|
|
|
// placeholder channel output.
|
2018-01-30 01:39:41 +08:00
|
|
|
BlackholeChan = make(chan interface{})
|
2016-10-13 23:44:28 +08:00
|
|
|
// DevNull is /dev/null.
|
|
|
|
DevNull *os.File
|
2019-04-19 05:15:34 +08:00
|
|
|
// DevNullClosedChan is a port made up from DevNull and ClosedChan,
|
2016-10-13 23:44:28 +08:00
|
|
|
// suitable as placeholder input port.
|
|
|
|
DevNullClosedChan *Port
|
2016-10-10 22:54:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-10-13 23:44:28 +08:00
|
|
|
close(ClosedChan)
|
|
|
|
go func() {
|
|
|
|
for range BlackholeChan {
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-10-10 22:54:58 +08:00
|
|
|
var err error
|
2017-12-04 05:47:12 +08:00
|
|
|
DevNull, err = os.Open(os.DevNull)
|
2016-10-10 22:54:58 +08:00
|
|
|
if err != nil {
|
2017-12-04 05:47:12 +08:00
|
|
|
os.Stderr.WriteString("cannot open " + os.DevNull + ", shell might not function normally\n")
|
2016-10-10 22:54:58 +08:00
|
|
|
}
|
2016-10-13 23:44:28 +08:00
|
|
|
DevNullClosedChan = &Port{File: DevNull, Chan: ClosedChan}
|
2016-10-10 22:54:58 +08:00
|
|
|
}
|