eval: Introduce (*Port).Fork.

This commit is contained in:
Qi Xiao 2016-02-09 01:40:36 +01:00
parent 4ff3f771d1
commit 6097cfd76d
3 changed files with 10 additions and 10 deletions

View File

@ -340,11 +340,7 @@ func (cp *compiler) redir(n *parse.Redir) Op {
ec.ports[dst] = &Port{}
} else {
fd := srcMust.zerothMustNonNegativeInt()
ec.ports[dst] = ec.ports[fd]
if ec.ports[dst] != nil {
ec.ports[dst].CloseFile = false
ec.ports[dst].CloseChan = false
}
ec.ports[dst] = ec.ports[fd].Fork()
}
} else {
f, err := os.OpenFile(src, flag, defaultFileRedirPerm)

View File

@ -111,13 +111,12 @@ func NewTopEvalCtx(ev *Evaler, name, text string, ports []*Port) *evalCtx {
}
}
// fork returns a modified copy of ec. The ports are copied deeply, with
// shouldClose flags reset, and the context is changed to the given value.
// Other fields are copied shallowly.
// fork returns a modified copy of ec. The ports are forked, and the context is
// changed to the given value. Other fields are copied shallowly.
func (ec *evalCtx) fork(newContext string) *evalCtx {
newPorts := make([]*Port, len(ec.ports))
for i, p := range ec.ports {
newPorts[i] = &Port{p.File, p.Chan, false, false}
newPorts[i] = p.Fork()
}
return &evalCtx{
ec.Evaler,

View File

@ -10,7 +10,12 @@ type Port struct {
CloseChan bool
}
// close closes a Port.
// 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