Skip to content

Commit

Permalink
interp: unexport Open,Exec,Stdin,Stdout,Stderr from Runner
Browse files Browse the repository at this point in the history
These fields are supposed to be modified via calling options
and there is little reason to read from them.

Fixes #428
  • Loading branch information
kaey authored and mvdan committed Oct 9, 2019
1 parent e0d0dfd commit 53fc6e6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 58 deletions.
2 changes: 1 addition & 1 deletion interp/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ func (r *Runner) readLine(raw bool) ([]byte, error) {

for {
var buf [1]byte
n, err := r.Stdin.Read(buf[:])
n, err := r.stdin.Read(buf[:])
if n > 0 {
b := buf[0]
switch {
Expand Down
114 changes: 57 additions & 57 deletions interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ type RunnerOption func(*Runner) error
// standard output writer means that the output will be discarded.
func New(opts ...RunnerOption) (*Runner, error) {
r := &Runner{
usedNew: true,
Exec: DefaultExec,
Open: DefaultOpen,
usedNew: true,
execModule: DefaultExec,
openModule: DefaultOpen,
}
r.dirStack = r.dirBootstrap[:0]
for _, opt := range opts {
Expand All @@ -58,8 +58,8 @@ func New(opts ...RunnerOption) (*Runner, error) {
return nil, err
}
}
if r.Stdout == nil || r.Stderr == nil {
StdIO(r.Stdin, r.Stdout, r.Stderr)(r)
if r.stdout == nil || r.stderr == nil {
StdIO(r.stdin, r.stdout, r.stderr)(r)
}
return r, nil
}
Expand All @@ -86,7 +86,7 @@ func (r *Runner) fillExpandConfig(ctx context.Context) {
return err
}
r2 := r.sub()
r2.Stdout = w
r2.stdout = w
r2.stmts(ctx, cs.Stmts)
return r2.err
},
Expand Down Expand Up @@ -283,15 +283,15 @@ func Params(args ...string) RunnerOption {
// ExecModule sets command execution handler. See ExecModuleFunc for more info.
func ExecModule(f ExecModuleFunc) RunnerOption {
return func(r *Runner) error {
r.Exec = f
r.execModule = f
return nil
}
}

// OpenModule sets file open handler. See OpenModuleFunc for more info.
func OpenModule(f OpenModuleFunc) RunnerOption {
return func(r *Runner) error {
r.Open = f
r.openModule = f
return nil
}
}
Expand All @@ -301,15 +301,15 @@ func OpenModule(f OpenModuleFunc) RunnerOption {
// the output.
func StdIO(in io.Reader, out, err io.Writer) RunnerOption {
return func(r *Runner) error {
r.Stdin = in
r.stdin = in
if out == nil {
out = ioutil.Discard
}
r.Stdout = out
r.stdout = out
if err == nil {
err = ioutil.Discard
}
r.Stderr = err
r.stderr = err
return nil
}
}
Expand Down Expand Up @@ -338,22 +338,22 @@ type Runner struct {
// file or calling a function. Accessible via the $@/$* family of vars.
Params []string

// Exec is the module responsible for executing programs. It must be
// non-nil.
Exec ExecModuleFunc
// Open is the module responsible for opening files. It must be non-nil.
Open OpenModuleFunc

Stdin io.Reader
Stdout io.Writer
Stderr io.Writer

// Separate maps - note that bash allows a name to be both a var and a
// func simultaneously

Vars map[string]expand.Variable
Funcs map[string]*syntax.Stmt

// execModule is the module responsible for executing programs. It must be non-nil.
execModule ExecModuleFunc

// openModule is the module responsible for opening files. It must be non-nil.
openModule OpenModuleFunc

stdin io.Reader
stdout io.Writer
stderr io.Writer

ecfg *expand.Config
ectx context.Context // just so that Runner.Sub can use it again

Expand Down Expand Up @@ -511,15 +511,15 @@ func (r *Runner) Reset() {
r.origDir = r.Dir
r.origParams = r.Params
r.origOpts = r.opts
r.origStdin = r.Stdin
r.origStdout = r.Stdout
r.origStderr = r.Stderr
r.origStdin = r.stdin
r.origStdout = r.stdout
r.origStderr = r.stderr
}
// reset the internal state
*r = Runner{
Env: r.Env,
Exec: r.Exec,
Open: r.Open,
execModule: r.execModule,
openModule: r.openModule,
KillTimeout: r.KillTimeout,

// These can be set by functions like Dir or Params, but
Expand All @@ -528,9 +528,9 @@ func (r *Runner) Reset() {
Dir: r.origDir,
Params: r.origParams,
opts: r.origOpts,
Stdin: r.origStdin,
Stdout: r.origStdout,
Stderr: r.origStderr,
stdin: r.origStdin,
stdout: r.origStdout,
stderr: r.origStderr,

origDir: r.origDir,
origParams: r.origParams,
Expand Down Expand Up @@ -591,9 +591,9 @@ func (r *Runner) Reset() {
func (r *Runner) modCtx(ctx context.Context) context.Context {
mc := ModuleCtx{
Dir: r.Dir,
Stdin: r.Stdin,
Stdout: r.Stdout,
Stderr: r.Stderr,
Stdin: r.stdin,
Stdout: r.stdout,
Stderr: r.stderr,
KillTimeout: r.KillTimeout,
}
oenv := overlayEnviron{
Expand Down Expand Up @@ -665,15 +665,15 @@ func (r *Runner) Exited() bool {
}

func (r *Runner) out(s string) {
io.WriteString(r.Stdout, s)
io.WriteString(r.stdout, s)
}

func (r *Runner) outf(format string, a ...interface{}) {
fmt.Fprintf(r.Stdout, format, a...)
fmt.Fprintf(r.stdout, format, a...)
}

func (r *Runner) errf(format string, a ...interface{}) {
fmt.Fprintf(r.Stderr, format, a...)
fmt.Fprintf(r.stderr, format, a...)
}

func (r *Runner) stop(ctx context.Context) bool {
Expand Down Expand Up @@ -707,7 +707,7 @@ func (r *Runner) stmt(ctx context.Context, st *syntax.Stmt) {
}

func (r *Runner) stmtSync(ctx context.Context, st *syntax.Stmt) {
oldIn, oldOut, oldErr := r.Stdin, r.Stdout, r.Stderr
oldIn, oldOut, oldErr := r.stdin, r.stdout, r.stderr
for _, rd := range st.Redirs {
cls, err := r.redir(ctx, rd)
if err != nil {
Expand Down Expand Up @@ -736,7 +736,7 @@ func (r *Runner) stmtSync(ctx context.Context, st *syntax.Stmt) {
r.exitShell = true
}
if !r.keepRedirs {
r.Stdin, r.Stdout, r.Stderr = oldIn, oldOut, oldErr
r.stdin, r.stdout, r.stderr = oldIn, oldOut, oldErr
}
}

Expand All @@ -747,12 +747,12 @@ func (r *Runner) sub() *Runner {
Env: r.Env,
Dir: r.Dir,
Params: r.Params,
Exec: r.Exec,
Open: r.Open,
Stdin: r.Stdin,
Stdout: r.Stdout,
Stderr: r.Stderr,
Funcs: r.Funcs,
execModule: r.execModule,
openModule: r.openModule,
stdin: r.stdin,
stdout: r.stdout,
stderr: r.stderr,
KillTimeout: r.KillTimeout,
filename: r.filename,
opts: r.opts,
Expand Down Expand Up @@ -821,14 +821,14 @@ func (r *Runner) cmd(ctx context.Context, cm syntax.Command) {
case syntax.Pipe, syntax.PipeAll:
pr, pw := io.Pipe()
r2 := r.sub()
r2.Stdout = pw
r2.stdout = pw
if x.Op == syntax.PipeAll {
r2.Stderr = pw
r2.stderr = pw
} else {
r2.Stderr = r.Stderr
r2.stderr = r.stderr
}
r.bufCopier.Reader = pr
r.Stdin = &r.bufCopier
r.stdin = &r.bufCopier
var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand Down Expand Up @@ -1088,28 +1088,28 @@ func (r *Runner) hdocReader(rd *syntax.Redirect) io.Reader {

func (r *Runner) redir(ctx context.Context, rd *syntax.Redirect) (io.Closer, error) {
if rd.Hdoc != nil {
r.Stdin = r.hdocReader(rd)
r.stdin = r.hdocReader(rd)
return nil, nil
}
orig := &r.Stdout
orig := &r.stdout
if rd.N != nil {
switch rd.N.Value {
case "1":
case "2":
orig = &r.Stderr
orig = &r.stderr
}
}
arg := r.literal(rd.Word)
switch rd.Op {
case syntax.WordHdoc:
r.Stdin = strings.NewReader(arg + "\n")
r.stdin = strings.NewReader(arg + "\n")
return nil, nil
case syntax.DplOut:
switch arg {
case "1":
*orig = r.Stdout
*orig = r.stdout
case "2":
*orig = r.Stderr
*orig = r.stderr
}
return nil, nil
case syntax.RdrIn, syntax.RdrOut, syntax.AppOut,
Expand All @@ -1132,12 +1132,12 @@ func (r *Runner) redir(ctx context.Context, rd *syntax.Redirect) (io.Closer, err
}
switch rd.Op {
case syntax.RdrIn:
r.Stdin = f
r.stdin = f
case syntax.RdrOut, syntax.AppOut:
*orig = f
case syntax.RdrAll, syntax.AppAll:
r.Stdout = f
r.Stderr = f
r.stdout = f
r.stderr = f
default:
panic(fmt.Sprintf("unhandled redirect op: %v", rd.Op))
}
Expand Down Expand Up @@ -1199,7 +1199,7 @@ func (r *Runner) call(ctx context.Context, pos syntax.Pos, args []string) {
}

func (r *Runner) exec(ctx context.Context, args []string) {
err := r.Exec(r.modCtx(ctx), args)
err := r.execModule(r.modCtx(ctx), args)
switch x := err.(type) {
case nil:
r.exit = 0
Expand All @@ -1211,7 +1211,7 @@ func (r *Runner) exec(ctx context.Context, args []string) {
}

func (r *Runner) open(ctx context.Context, path string, flags int, mode os.FileMode, print bool) (io.ReadWriteCloser, error) {
f, err := r.Open(r.modCtx(ctx), path, flags, mode)
f, err := r.openModule(r.modCtx(ctx), path, flags, mode)
switch err.(type) {
case nil:
case *os.PathError:
Expand Down

0 comments on commit 53fc6e6

Please sign in to comment.