-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
port.go
59 lines (52 loc) · 1.25 KB
/
port.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package eval
import (
"os"
)
// Port conveys data stream. It always consists of a byte band and a channel band.
type Port struct {
File *os.File
Chan chan interface{}
CloseFile bool
CloseChan bool
}
// 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.
func (p *Port) Close() {
if p == nil {
return
}
if p.CloseFile {
p.File.Close()
}
if p.CloseChan {
close(p.Chan)
}
}
var (
// ClosedChan is a closed channel, suitable for use as placeholder channel input.
ClosedChan = make(chan interface{})
// BlackholeChan is channel writes onto which disappear, suitable for use as
// placeholder channel output.
BlackholeChan = make(chan interface{})
// DevNull is /dev/null.
DevNull *os.File
// DevNullClosedInput is a port made up from DevNull and ClosedChan,
// suitable as placeholder input port.
DevNullClosedChan *Port
)
func init() {
close(ClosedChan)
go func() {
for range BlackholeChan {
}
}()
var err error
DevNull, err = os.Open(os.DevNull)
if err != nil {
os.Stderr.WriteString("cannot open " + os.DevNull + ", shell might not function normally\n")
}
DevNullClosedChan = &Port{File: DevNull, Chan: ClosedChan}
}