Skip to content

Commit

Permalink
[#859]Support named FD in redirections (#869)
Browse files Browse the repository at this point in the history
* [#859]Support named FD in redirections

* changes based on code reviews

* change error var name
  • Loading branch information
jiujieti authored and xiaq committed Nov 16, 2019
1 parent 16d0bd3 commit aa52b2d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion eval/compile_effect.go
Expand Up @@ -493,7 +493,7 @@ func (op *redirOp) invoke(fm *Frame) error {
} else {
var err error
// dst must be a valid fd
dst, err = fm.ExecAndUnwrap("Fd", op.dstOp).One().NonNegativeInt()
dst, err = fm.ExecAndUnwrap("Fd", op.dstOp).One().Fd()
if err != nil {
return err
}
Expand Down
27 changes: 26 additions & 1 deletion eval/unwrap.go
Expand Up @@ -97,12 +97,37 @@ func (u ValueUnwrapper) NonNegativeInt() (int, error) {
return i, u.err
}

func (u ValueUnwrapper) Fd() (int, error) {
s, err := u.String()
if err != nil {
return 0, err
}
switch s {
case "stdin":
return 0, nil
case "stdout":
return 1, nil
case "stderr":
return 2, nil
default:
i, err := u.NonNegativeInt()
if err != nil {
return 0, u.ctx.errorpf(u.begin, u.end, "fd must be standard stream name or integer; got %s", s)
}
return i, nil
}
}

func (u ValueUnwrapper) FdOrClose() (int, error) {
s, err := u.String()
if err == nil && s == "-" {
return -1, nil
}
return u.NonNegativeInt()
fd, err := u.Fd()
if err != nil {
return 0, u.ctx.errorpf(u.begin, u.end, "redirection source must be standard stream name or integer; got %s", s)
}
return fd, nil
}

func (u ValueUnwrapper) Callable() (Callable, error) {
Expand Down

0 comments on commit aa52b2d

Please sign in to comment.