2017-04-24 14:47:10 +02:00
|
|
|
// Copyright (c) 2017, Daniel Martí <mvdan@mvdan.cc>
|
|
|
|
// See LICENSE for licensing information
|
|
|
|
|
|
|
|
package interp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-09-07 19:02:52 +02:00
|
|
|
"math"
|
2017-04-24 14:47:10 +02:00
|
|
|
"os"
|
|
|
|
"os/user"
|
2017-06-14 19:00:36 +02:00
|
|
|
"path/filepath"
|
2018-01-03 19:12:40 +02:00
|
|
|
"runtime"
|
2017-04-24 14:47:10 +02:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2017-09-07 19:02:52 +02:00
|
|
|
"time"
|
2017-04-24 14:47:10 +02:00
|
|
|
|
2017-09-02 16:19:00 +02:00
|
|
|
"mvdan.cc/sh/syntax"
|
2017-04-24 14:47:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Runner interprets shell programs. It cannot be reused once a
|
|
|
|
// program has been interpreted.
|
|
|
|
//
|
|
|
|
// Note that writes to Stdout and Stderr may not be sequential. If
|
|
|
|
// you plan on using an io.Writer implementation that isn't safe for
|
|
|
|
// concurrent use, consider a workaround like hiding writes behind a
|
|
|
|
// mutex.
|
|
|
|
type Runner struct {
|
|
|
|
// Env specifies the environment of the interpreter.
|
|
|
|
// If Env is nil, Run uses the current process's environment.
|
2018-04-28 20:35:15 +02:00
|
|
|
Env Environ
|
2017-04-24 14:47:10 +02:00
|
|
|
|
|
|
|
// Dir specifies the working directory of the command. If Dir is
|
|
|
|
// the empty string, Run runs the command in the calling
|
|
|
|
// process's current directory.
|
|
|
|
Dir string
|
|
|
|
|
2017-06-24 21:00:10 +02:00
|
|
|
// Params are the current parameters, e.g. from running a shell
|
|
|
|
// file or calling a function. Accessible via the $@/$* family
|
|
|
|
// of vars.
|
|
|
|
Params []string
|
|
|
|
|
2017-10-15 21:41:15 +02:00
|
|
|
Exec ModuleExec
|
|
|
|
Open ModuleOpen
|
|
|
|
|
2017-08-05 19:13:35 +02:00
|
|
|
filename string // only if Node was a File
|
|
|
|
|
2017-04-24 14:47:10 +02:00
|
|
|
// Separate maps, note that bash allows a name to be both a var
|
|
|
|
// and a func simultaneously
|
2018-01-03 19:12:40 +02:00
|
|
|
Vars map[string]Variable
|
|
|
|
Funcs map[string]*syntax.Stmt
|
2017-04-24 14:47:10 +02:00
|
|
|
|
2018-01-03 19:12:40 +02:00
|
|
|
// like Vars, but local to a func i.e. "local foo=bar"
|
|
|
|
funcVars map[string]Variable
|
|
|
|
|
|
|
|
// like Vars, but local to a cmd i.e. "foo=bar prog args..."
|
|
|
|
cmdVars map[string]string
|
2017-04-24 14:47:10 +02:00
|
|
|
|
|
|
|
// >0 to break or continue out of N enclosing loops
|
|
|
|
breakEnclosing, contnEnclosing int
|
|
|
|
|
2018-01-03 19:12:40 +02:00
|
|
|
inLoop bool
|
|
|
|
inFunc bool
|
|
|
|
inSource bool
|
2017-04-24 14:47:10 +02:00
|
|
|
|
|
|
|
err error // current fatal error
|
|
|
|
exit int // current (last) exit code
|
|
|
|
|
|
|
|
Stdin io.Reader
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
|
|
|
|
|
|
|
bgShells sync.WaitGroup
|
|
|
|
|
|
|
|
// Context can be used to cancel the interpreter before it finishes
|
|
|
|
Context context.Context
|
2017-06-24 21:00:10 +02:00
|
|
|
|
2018-04-28 20:35:15 +02:00
|
|
|
opts [len(shellOptsTable) + len(bashOptsTable)]bool
|
2017-11-19 22:26:37 +02:00
|
|
|
|
|
|
|
dirStack []string
|
|
|
|
|
2018-01-03 19:12:40 +02:00
|
|
|
optState getopts
|
|
|
|
|
|
|
|
ifsJoin string
|
|
|
|
ifsRune func(rune) bool
|
|
|
|
|
|
|
|
// keepRedirs is used so that "exec" can make any redirections
|
|
|
|
// apply to the current shell, and not just the command.
|
|
|
|
keepRedirs bool
|
|
|
|
|
2017-11-19 22:26:37 +02:00
|
|
|
// KillTimeout holds how much time the interpreter will wait for a
|
|
|
|
// program to stop after being sent an interrupt signal, after
|
|
|
|
// which a kill signal will be sent. This process will happen when the
|
|
|
|
// interpreter's context is cancelled.
|
|
|
|
//
|
|
|
|
// The zero value will default to 2 seconds.
|
|
|
|
//
|
|
|
|
// A negative value means that a kill signal will be sent immediately.
|
|
|
|
//
|
|
|
|
// On Windows, the kill signal is always sent immediately,
|
|
|
|
// because Go doesn't currently support sending Interrupt on Windows.
|
|
|
|
KillTimeout time.Duration
|
2018-01-03 19:12:40 +02:00
|
|
|
|
2018-04-28 20:35:15 +02:00
|
|
|
fieldAlloc [4]fieldPart
|
|
|
|
fieldsAlloc [4][]fieldPart
|
|
|
|
bufferAlloc bytes.Buffer
|
|
|
|
oneWord [1]*syntax.Word
|
2018-01-03 19:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) strBuilder() *bytes.Buffer {
|
|
|
|
b := &r.bufferAlloc
|
|
|
|
b.Reset()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) optByFlag(flag string) *bool {
|
|
|
|
for i, opt := range shellOptsTable {
|
|
|
|
if opt.flag == flag {
|
2018-04-28 20:35:15 +02:00
|
|
|
return &r.opts[i]
|
2018-01-03 19:12:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 20:35:15 +02:00
|
|
|
func (r *Runner) optByName(name string, bash bool) *bool {
|
|
|
|
if bash {
|
|
|
|
for i, optName := range bashOptsTable {
|
|
|
|
if optName == name {
|
|
|
|
return &r.opts[len(shellOptsTable)+i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-03 19:12:40 +02:00
|
|
|
for i, opt := range shellOptsTable {
|
|
|
|
if opt.name == name {
|
2018-04-28 20:35:15 +02:00
|
|
|
return &r.opts[i]
|
2018-01-03 19:12:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var shellOptsTable = [...]struct {
|
|
|
|
flag, name string
|
|
|
|
}{
|
|
|
|
// sorted alphabetically by name; use a space for the options
|
|
|
|
// that have no flag form
|
|
|
|
{"a", "allexport"},
|
|
|
|
{"e", "errexit"},
|
|
|
|
{"n", "noexec"},
|
|
|
|
{"f", "noglob"},
|
|
|
|
{"u", "nounset"},
|
|
|
|
{" ", "pipefail"},
|
|
|
|
}
|
|
|
|
|
2018-04-28 20:35:15 +02:00
|
|
|
var bashOptsTable = [...]string{
|
|
|
|
// sorted alphabetically by name
|
|
|
|
"globstar",
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:12:40 +02:00
|
|
|
// To access the shell options arrays without a linear search when we
|
2018-04-28 20:35:15 +02:00
|
|
|
// know which option we're after at compile time. First come the shell options,
|
|
|
|
// then the bash options.
|
2018-01-03 19:12:40 +02:00
|
|
|
const (
|
|
|
|
optAllExport = iota
|
|
|
|
optErrExit
|
|
|
|
optNoExec
|
|
|
|
optNoGlob
|
|
|
|
optNoUnset
|
|
|
|
optPipeFail
|
2018-04-28 20:35:15 +02:00
|
|
|
|
|
|
|
optGlobStar
|
2018-01-03 19:12:40 +02:00
|
|
|
)
|
|
|
|
|
2017-10-15 21:41:15 +02:00
|
|
|
// Reset will set the unexported fields back to zero, fill any exported
|
|
|
|
// fields with their default values if not set, and prepare the runner
|
|
|
|
// to interpret a program.
|
|
|
|
//
|
|
|
|
// This function should be called once before running any node. It can
|
|
|
|
// be skipped before any following runs to keep internal state, such as
|
|
|
|
// declared variables.
|
|
|
|
func (r *Runner) Reset() error {
|
|
|
|
// reset the internal state
|
|
|
|
*r = Runner{
|
2017-11-19 22:26:37 +02:00
|
|
|
Env: r.Env,
|
|
|
|
Dir: r.Dir,
|
|
|
|
Params: r.Params,
|
|
|
|
Context: r.Context,
|
|
|
|
Stdin: r.Stdin,
|
|
|
|
Stdout: r.Stdout,
|
|
|
|
Stderr: r.Stderr,
|
|
|
|
Exec: r.Exec,
|
|
|
|
Open: r.Open,
|
|
|
|
KillTimeout: r.KillTimeout,
|
2018-01-03 19:12:40 +02:00
|
|
|
|
|
|
|
// emptied below, to reuse the space
|
|
|
|
Vars: r.Vars,
|
|
|
|
cmdVars: r.cmdVars,
|
|
|
|
dirStack: r.dirStack[:0],
|
|
|
|
}
|
|
|
|
if r.Vars == nil {
|
|
|
|
r.Vars = make(map[string]Variable)
|
|
|
|
} else {
|
|
|
|
for k := range r.Vars {
|
|
|
|
delete(r.Vars, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.cmdVars == nil {
|
|
|
|
r.cmdVars = make(map[string]string)
|
|
|
|
} else {
|
|
|
|
for k := range r.cmdVars {
|
|
|
|
delete(r.cmdVars, k)
|
|
|
|
}
|
2017-10-15 21:41:15 +02:00
|
|
|
}
|
|
|
|
if r.Context == nil {
|
|
|
|
r.Context = context.Background()
|
|
|
|
}
|
|
|
|
if r.Env == nil {
|
2018-04-28 20:35:15 +02:00
|
|
|
r.Env, _ = EnvFromList(os.Environ())
|
2017-10-15 21:41:15 +02:00
|
|
|
}
|
2018-04-28 20:35:15 +02:00
|
|
|
if _, ok := r.Env.Get("HOME"); !ok {
|
2017-10-15 21:41:15 +02:00
|
|
|
u, _ := user.Current()
|
2018-01-03 19:12:40 +02:00
|
|
|
r.Vars["HOME"] = Variable{Value: StringVal(u.HomeDir)}
|
2017-10-15 21:41:15 +02:00
|
|
|
}
|
|
|
|
if r.Dir == "" {
|
|
|
|
dir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get current dir: %v", err)
|
|
|
|
}
|
|
|
|
r.Dir = dir
|
|
|
|
}
|
2018-01-03 19:12:40 +02:00
|
|
|
r.Vars["PWD"] = Variable{Value: StringVal(r.Dir)}
|
|
|
|
r.Vars["IFS"] = Variable{Value: StringVal(" \t\n")}
|
|
|
|
r.ifsUpdated()
|
|
|
|
r.Vars["OPTIND"] = Variable{Value: StringVal("1")}
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// convert $PATH to a unix path list
|
2018-04-28 20:35:15 +02:00
|
|
|
path, _ := r.Env.Get("PATH")
|
2018-01-03 19:12:40 +02:00
|
|
|
path = strings.Join(filepath.SplitList(path), ":")
|
|
|
|
r.Vars["PATH"] = Variable{Value: StringVal(path)}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.dirStack = append(r.dirStack, r.Dir)
|
2017-10-15 21:41:15 +02:00
|
|
|
if r.Exec == nil {
|
|
|
|
r.Exec = DefaultExec
|
|
|
|
}
|
|
|
|
if r.Open == nil {
|
|
|
|
r.Open = DefaultOpen
|
|
|
|
}
|
2017-11-19 22:26:37 +02:00
|
|
|
if r.KillTimeout == 0 {
|
|
|
|
r.KillTimeout = 2 * time.Second
|
|
|
|
}
|
2017-10-15 21:41:15 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) ctx() Ctxt {
|
|
|
|
c := Ctxt{
|
2017-11-19 22:26:37 +02:00
|
|
|
Context: r.Context,
|
|
|
|
Env: r.Env,
|
|
|
|
Dir: r.Dir,
|
|
|
|
Stdin: r.Stdin,
|
|
|
|
Stdout: r.Stdout,
|
|
|
|
Stderr: r.Stderr,
|
|
|
|
KillTimeout: r.KillTimeout,
|
2017-10-15 21:41:15 +02:00
|
|
|
}
|
2018-04-28 20:35:15 +02:00
|
|
|
c.Env = r.Env.Copy()
|
2018-01-03 19:12:40 +02:00
|
|
|
for name, vr := range r.Vars {
|
|
|
|
if !vr.Exported {
|
|
|
|
continue
|
2017-11-19 22:26:37 +02:00
|
|
|
}
|
2018-04-28 20:35:15 +02:00
|
|
|
c.Env.Set(name, r.varStr(vr, 0))
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
2018-01-03 19:12:40 +02:00
|
|
|
for name, val := range r.cmdVars {
|
2018-04-28 20:35:15 +02:00
|
|
|
c.Env.Set(name, val)
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
2018-01-03 19:12:40 +02:00
|
|
|
return c
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExitCode uint8
|
|
|
|
|
|
|
|
func (e ExitCode) Error() string { return fmt.Sprintf("exit status %d", e) }
|
|
|
|
|
2017-10-15 21:41:15 +02:00
|
|
|
func (r *Runner) setErr(err error) {
|
2017-04-24 14:47:10 +02:00
|
|
|
if r.err == nil {
|
2017-10-15 21:41:15 +02:00
|
|
|
r.err = err
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) lastExit() {
|
|
|
|
if r.err == nil {
|
|
|
|
r.err = ExitCode(r.exit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-24 21:00:10 +02:00
|
|
|
// FromArgs populates the shell options and returns the remaining
|
|
|
|
// arguments. For example, running FromArgs("-e", "--", "foo") will set
|
|
|
|
// the "-e" option and return []string{"foo"}.
|
|
|
|
//
|
|
|
|
// This is similar to what the interpreter's "set" builtin does.
|
|
|
|
func (r *Runner) FromArgs(args ...string) ([]string, error) {
|
|
|
|
for len(args) > 0 {
|
2018-01-03 19:12:40 +02:00
|
|
|
arg := args[0]
|
|
|
|
if arg == "" || (arg[0] != '-' && arg[0] != '+') {
|
2017-06-24 21:00:10 +02:00
|
|
|
break
|
|
|
|
}
|
2018-01-03 19:12:40 +02:00
|
|
|
if arg == "--" {
|
2017-06-24 21:00:10 +02:00
|
|
|
args = args[1:]
|
2018-01-03 19:12:40 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
enable := arg[0] == '-'
|
|
|
|
var opt *bool
|
|
|
|
if flag := arg[1:]; flag == "o" {
|
|
|
|
args = args[1:]
|
|
|
|
if len(args) == 0 && enable {
|
|
|
|
for i, opt := range shellOptsTable {
|
2018-04-28 20:35:15 +02:00
|
|
|
r.printOptLine(opt.name, r.opts[i])
|
2018-01-03 19:12:40 +02:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(args) == 0 && !enable {
|
|
|
|
for i, opt := range shellOptsTable {
|
|
|
|
setFlag := "+o"
|
2018-04-28 20:35:15 +02:00
|
|
|
if r.opts[i] {
|
2018-01-03 19:12:40 +02:00
|
|
|
setFlag = "-o"
|
|
|
|
}
|
|
|
|
r.outf("set %s %s\n", setFlag, opt.name)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2018-04-28 20:35:15 +02:00
|
|
|
opt = r.optByName(args[0], false)
|
2018-01-03 19:12:40 +02:00
|
|
|
} else {
|
|
|
|
opt = r.optByFlag(flag)
|
2017-06-24 21:00:10 +02:00
|
|
|
}
|
2018-01-03 19:12:40 +02:00
|
|
|
if opt == nil {
|
|
|
|
return nil, fmt.Errorf("invalid option: %q", arg)
|
|
|
|
}
|
|
|
|
*opt = enable
|
2017-06-24 21:00:10 +02:00
|
|
|
args = args[1:]
|
|
|
|
}
|
|
|
|
return args, nil
|
|
|
|
}
|
|
|
|
|
2017-08-05 19:13:35 +02:00
|
|
|
// Run starts the interpreter and returns any error.
|
|
|
|
func (r *Runner) Run(node syntax.Node) error {
|
|
|
|
r.filename = ""
|
|
|
|
switch x := node.(type) {
|
2017-07-31 00:11:34 +02:00
|
|
|
case *syntax.File:
|
2017-08-05 19:13:35 +02:00
|
|
|
r.filename = x.Name
|
2017-07-31 00:11:34 +02:00
|
|
|
r.stmts(x.StmtList)
|
|
|
|
case *syntax.Stmt:
|
|
|
|
r.stmt(x)
|
|
|
|
case syntax.Command:
|
|
|
|
r.cmd(x)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Node can only be File, Stmt, or Command: %T", x)
|
|
|
|
}
|
2017-04-24 14:47:10 +02:00
|
|
|
r.lastExit()
|
|
|
|
if r.err == ExitCode(0) {
|
|
|
|
r.err = nil
|
|
|
|
}
|
|
|
|
return r.err
|
|
|
|
}
|
|
|
|
|
2017-10-15 21:41:15 +02:00
|
|
|
func (r *Runner) Stmt(stmt *syntax.Stmt) error {
|
|
|
|
r.stmt(stmt)
|
|
|
|
return r.err
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:12:40 +02:00
|
|
|
func (r *Runner) out(s string) {
|
|
|
|
io.WriteString(r.Stdout, s)
|
|
|
|
}
|
|
|
|
|
2017-04-24 14:47:10 +02:00
|
|
|
func (r *Runner) outf(format string, a ...interface{}) {
|
|
|
|
fmt.Fprintf(r.Stdout, format, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) errf(format string, a ...interface{}) {
|
|
|
|
fmt.Fprintf(r.Stderr, format, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) stop() bool {
|
|
|
|
if r.err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err := r.Context.Err(); err != nil {
|
|
|
|
r.err = err
|
|
|
|
return true
|
|
|
|
}
|
2018-04-28 20:35:15 +02:00
|
|
|
if r.opts[optNoExec] {
|
2018-01-03 19:12:40 +02:00
|
|
|
return true
|
|
|
|
}
|
2017-04-24 14:47:10 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) stmt(st *syntax.Stmt) {
|
|
|
|
if r.stop() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if st.Background {
|
|
|
|
r.bgShells.Add(1)
|
2017-11-19 22:26:37 +02:00
|
|
|
r2 := r.sub()
|
2017-04-24 14:47:10 +02:00
|
|
|
go func() {
|
|
|
|
r2.stmtSync(st)
|
|
|
|
r.bgShells.Done()
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
r.stmtSync(st)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) stmtSync(st *syntax.Stmt) {
|
|
|
|
oldIn, oldOut, oldErr := r.Stdin, r.Stdout, r.Stderr
|
|
|
|
for _, rd := range st.Redirs {
|
|
|
|
cls, err := r.redir(rd)
|
|
|
|
if err != nil {
|
|
|
|
r.exit = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if cls != nil {
|
|
|
|
defer cls.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if st.Cmd == nil {
|
|
|
|
r.exit = 0
|
|
|
|
} else {
|
|
|
|
r.cmd(st.Cmd)
|
|
|
|
}
|
|
|
|
if st.Negated {
|
|
|
|
r.exit = oneIf(r.exit == 0)
|
|
|
|
}
|
2018-04-28 20:35:15 +02:00
|
|
|
if r.exit != 0 && r.opts[optErrExit] {
|
2018-01-03 19:12:40 +02:00
|
|
|
r.lastExit()
|
|
|
|
}
|
|
|
|
if !r.keepRedirs {
|
|
|
|
r.Stdin, r.Stdout, r.Stderr = oldIn, oldOut, oldErr
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-19 22:26:37 +02:00
|
|
|
func (r *Runner) sub() *Runner {
|
|
|
|
r2 := *r
|
|
|
|
r2.bgShells = sync.WaitGroup{}
|
2018-01-03 19:12:40 +02:00
|
|
|
r2.bufferAlloc = bytes.Buffer{}
|
2017-11-19 22:26:37 +02:00
|
|
|
// TODO: perhaps we could do a lazy copy here, or some sort of
|
|
|
|
// overlay to avoid copying all the time
|
2018-04-28 20:35:15 +02:00
|
|
|
r2.Env = r.Env.Copy()
|
2018-01-03 19:12:40 +02:00
|
|
|
r2.Vars = make(map[string]Variable, len(r.Vars))
|
|
|
|
for k, v := range r.Vars {
|
|
|
|
r2.Vars[k] = v
|
|
|
|
}
|
|
|
|
r2.cmdVars = make(map[string]string, len(r.cmdVars))
|
|
|
|
for k, v := range r.cmdVars {
|
|
|
|
r2.cmdVars[k] = v
|
2017-11-19 22:26:37 +02:00
|
|
|
}
|
|
|
|
return &r2
|
|
|
|
}
|
|
|
|
|
2017-04-24 14:47:10 +02:00
|
|
|
func (r *Runner) cmd(cm syntax.Command) {
|
|
|
|
if r.stop() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch x := cm.(type) {
|
|
|
|
case *syntax.Block:
|
2017-07-06 01:46:05 +02:00
|
|
|
r.stmts(x.StmtList)
|
2017-04-24 14:47:10 +02:00
|
|
|
case *syntax.Subshell:
|
2017-11-19 22:26:37 +02:00
|
|
|
r2 := r.sub()
|
2017-07-06 01:46:05 +02:00
|
|
|
r2.stmts(x.StmtList)
|
2017-04-24 14:47:10 +02:00
|
|
|
r.exit = r2.exit
|
2017-10-15 21:41:15 +02:00
|
|
|
r.setErr(r2.err)
|
2017-04-24 14:47:10 +02:00
|
|
|
case *syntax.CallExpr:
|
2018-01-03 19:12:40 +02:00
|
|
|
fields := r.Fields(x.Args...)
|
2017-11-19 22:26:37 +02:00
|
|
|
if len(fields) == 0 {
|
2017-07-31 00:11:34 +02:00
|
|
|
for _, as := range x.Assigns {
|
2018-01-03 19:12:40 +02:00
|
|
|
vr, _ := r.lookupVar(as.Name.Value)
|
|
|
|
vr.Value = r.assignVal(as, "")
|
|
|
|
r.setVar(as.Name.Value, as.Index, vr)
|
2017-07-31 00:11:34 +02:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for _, as := range x.Assigns {
|
2018-01-03 19:12:40 +02:00
|
|
|
val := r.assignVal(as, "")
|
|
|
|
// we know that inline vars must be strings
|
|
|
|
r.cmdVars[as.Name.Value] = string(val.(StringVal))
|
|
|
|
if as.Name.Value == "IFS" {
|
|
|
|
r.ifsUpdated()
|
|
|
|
defer r.ifsUpdated()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.call(x.Args[0].Pos(), fields)
|
|
|
|
// cmdVars can be nuked here, as they are never useful
|
|
|
|
// again once we nest into further levels of inline
|
|
|
|
// vars.
|
|
|
|
for k := range r.cmdVars {
|
|
|
|
delete(r.cmdVars, k)
|
2017-07-31 00:11:34 +02:00
|
|
|
}
|
2017-04-24 14:47:10 +02:00
|
|
|
case *syntax.BinaryCmd:
|
|
|
|
switch x.Op {
|
|
|
|
case syntax.AndStmt:
|
|
|
|
r.stmt(x.X)
|
|
|
|
if r.exit == 0 {
|
|
|
|
r.stmt(x.Y)
|
|
|
|
}
|
|
|
|
case syntax.OrStmt:
|
|
|
|
r.stmt(x.X)
|
|
|
|
if r.exit != 0 {
|
|
|
|
r.stmt(x.Y)
|
|
|
|
}
|
|
|
|
case syntax.Pipe, syntax.PipeAll:
|
|
|
|
pr, pw := io.Pipe()
|
2017-11-19 22:26:37 +02:00
|
|
|
r2 := r.sub()
|
2017-04-24 14:47:10 +02:00
|
|
|
r2.Stdout = pw
|
|
|
|
if x.Op == syntax.PipeAll {
|
|
|
|
r2.Stderr = pw
|
|
|
|
} else {
|
|
|
|
r2.Stderr = r.Stderr
|
|
|
|
}
|
|
|
|
r.Stdin = pr
|
2017-10-15 21:41:15 +02:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2017-04-24 14:47:10 +02:00
|
|
|
go func() {
|
|
|
|
r2.stmt(x.X)
|
|
|
|
pw.Close()
|
2017-10-15 21:41:15 +02:00
|
|
|
wg.Done()
|
2017-04-24 14:47:10 +02:00
|
|
|
}()
|
|
|
|
r.stmt(x.Y)
|
|
|
|
pr.Close()
|
2017-10-15 21:41:15 +02:00
|
|
|
wg.Wait()
|
2018-04-28 20:35:15 +02:00
|
|
|
if r.opts[optPipeFail] && r2.exit > 0 && r.exit == 0 {
|
2018-01-03 19:12:40 +02:00
|
|
|
r.exit = r2.exit
|
|
|
|
}
|
2017-10-15 21:41:15 +02:00
|
|
|
r.setErr(r2.err)
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
case *syntax.IfClause:
|
2017-07-06 01:46:05 +02:00
|
|
|
r.stmts(x.Cond)
|
2017-04-24 14:47:10 +02:00
|
|
|
if r.exit == 0 {
|
2017-07-06 01:46:05 +02:00
|
|
|
r.stmts(x.Then)
|
2018-01-03 19:12:40 +02:00
|
|
|
break
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
2017-05-01 00:50:22 +02:00
|
|
|
r.exit = 0
|
2017-07-06 01:46:05 +02:00
|
|
|
r.stmts(x.Else)
|
2017-04-24 14:47:10 +02:00
|
|
|
case *syntax.WhileClause:
|
2018-01-03 19:12:40 +02:00
|
|
|
for !r.stop() {
|
2017-07-06 01:46:05 +02:00
|
|
|
r.stmts(x.Cond)
|
2017-05-17 19:49:27 +02:00
|
|
|
stop := (r.exit == 0) == x.Until
|
2017-04-24 14:47:10 +02:00
|
|
|
r.exit = 0
|
2017-07-06 01:46:05 +02:00
|
|
|
if stop || r.loopStmtsBroken(x.Do) {
|
2017-04-24 14:47:10 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *syntax.ForClause:
|
|
|
|
switch y := x.Loop.(type) {
|
|
|
|
case *syntax.WordIter:
|
|
|
|
name := y.Name.Value
|
2018-01-03 19:12:40 +02:00
|
|
|
for _, field := range r.Fields(y.Items...) {
|
|
|
|
r.setVarString(name, field)
|
2017-07-06 01:46:05 +02:00
|
|
|
if r.loopStmtsBroken(x.Do) {
|
2017-04-24 14:47:10 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *syntax.CStyleLoop:
|
|
|
|
r.arithm(y.Init)
|
|
|
|
for r.arithm(y.Cond) != 0 {
|
2017-07-06 01:46:05 +02:00
|
|
|
if r.loopStmtsBroken(x.Do) {
|
2017-04-24 14:47:10 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
r.arithm(y.Post)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *syntax.FuncDecl:
|
|
|
|
r.setFunc(x.Name.Value, x.Body)
|
|
|
|
case *syntax.ArithmCmd:
|
2018-01-03 19:12:40 +02:00
|
|
|
r.exit = oneIf(r.arithm(x.X) == 0)
|
2017-04-24 14:47:10 +02:00
|
|
|
case *syntax.LetClause:
|
|
|
|
var val int
|
|
|
|
for _, expr := range x.Exprs {
|
|
|
|
val = r.arithm(expr)
|
|
|
|
}
|
2018-01-03 19:12:40 +02:00
|
|
|
r.exit = oneIf(val == 0)
|
2017-04-24 14:47:10 +02:00
|
|
|
case *syntax.CaseClause:
|
|
|
|
str := r.loneWord(x.Word)
|
2017-05-27 16:17:49 +02:00
|
|
|
for _, ci := range x.Items {
|
|
|
|
for _, word := range ci.Patterns {
|
2018-01-03 19:12:40 +02:00
|
|
|
pat := r.lonePattern(word)
|
|
|
|
if match(pat, str) {
|
2017-07-06 01:46:05 +02:00
|
|
|
r.stmts(ci.StmtList)
|
2017-04-24 14:47:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *syntax.TestClause:
|
2018-01-03 19:12:40 +02:00
|
|
|
r.exit = 0
|
|
|
|
if r.bashTest(x.X) == "" && r.exit == 0 {
|
|
|
|
// to preserve exit code 2 for regex
|
|
|
|
// errors, etc
|
|
|
|
r.exit = 1
|
|
|
|
}
|
|
|
|
case *syntax.DeclClause:
|
|
|
|
local := false
|
|
|
|
var modes []string
|
|
|
|
valType := ""
|
|
|
|
switch x.Variant.Value {
|
|
|
|
case "declare":
|
|
|
|
// When used in a function, "declare" acts as
|
|
|
|
// "local" unless the "-g" option is used.
|
|
|
|
local = r.inFunc
|
|
|
|
case "local":
|
|
|
|
if !r.inFunc {
|
|
|
|
r.errf("local: can only be used in a function\n")
|
2017-11-19 22:26:37 +02:00
|
|
|
r.exit = 1
|
2018-01-03 19:12:40 +02:00
|
|
|
return
|
2017-11-19 22:26:37 +02:00
|
|
|
}
|
2018-01-03 19:12:40 +02:00
|
|
|
local = true
|
|
|
|
case "export":
|
|
|
|
modes = append(modes, "-x")
|
|
|
|
case "readonly":
|
|
|
|
modes = append(modes, "-r")
|
|
|
|
case "nameref":
|
|
|
|
modes = append(modes, "-n")
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
2017-11-19 22:26:37 +02:00
|
|
|
for _, opt := range x.Opts {
|
|
|
|
switch s := r.loneWord(opt); s {
|
2018-01-03 19:12:40 +02:00
|
|
|
case "-x", "-r", "-n":
|
|
|
|
modes = append(modes, s)
|
|
|
|
case "-a", "-A":
|
|
|
|
valType = s
|
|
|
|
case "-g":
|
|
|
|
local = false
|
2017-11-19 22:26:37 +02:00
|
|
|
default:
|
2018-01-03 19:12:40 +02:00
|
|
|
r.errf("declare: invalid option %q\n", s)
|
|
|
|
r.exit = 2
|
|
|
|
return
|
2017-11-19 22:26:37 +02:00
|
|
|
}
|
2017-05-27 16:17:49 +02:00
|
|
|
}
|
|
|
|
for _, as := range x.Assigns {
|
2018-01-03 19:12:40 +02:00
|
|
|
for _, as := range r.expandAssigns(as) {
|
|
|
|
name := as.Name.Value
|
|
|
|
vr, _ := r.lookupVar(as.Name.Value)
|
|
|
|
vr.Value = r.assignVal(as, valType)
|
|
|
|
vr.Local = local
|
|
|
|
for _, mode := range modes {
|
|
|
|
switch mode {
|
|
|
|
case "-x":
|
|
|
|
vr.Exported = true
|
|
|
|
case "-r":
|
|
|
|
vr.ReadOnly = true
|
|
|
|
case "-n":
|
|
|
|
vr.NameRef = true
|
|
|
|
}
|
2017-11-19 22:26:37 +02:00
|
|
|
}
|
2018-01-03 19:12:40 +02:00
|
|
|
r.setVar(name, as.Index, vr)
|
2017-11-19 22:26:37 +02:00
|
|
|
}
|
2017-05-27 16:17:49 +02:00
|
|
|
}
|
2017-09-07 19:02:52 +02:00
|
|
|
case *syntax.TimeClause:
|
|
|
|
start := time.Now()
|
|
|
|
if x.Stmt != nil {
|
|
|
|
r.stmt(x.Stmt)
|
|
|
|
}
|
2018-01-03 19:12:40 +02:00
|
|
|
format := "%s\t%s\n"
|
|
|
|
if x.PosixFormat {
|
|
|
|
format = "%s %s\n"
|
|
|
|
} else {
|
|
|
|
r.outf("\n")
|
|
|
|
}
|
2017-10-15 21:41:15 +02:00
|
|
|
real := time.Since(start)
|
2018-01-03 19:12:40 +02:00
|
|
|
r.outf(format, "real", elapsedString(real, x.PosixFormat))
|
2017-09-07 19:02:52 +02:00
|
|
|
// TODO: can we do these?
|
2018-01-03 19:12:40 +02:00
|
|
|
r.outf(format, "user", elapsedString(0, x.PosixFormat))
|
|
|
|
r.outf(format, "sys", elapsedString(0, x.PosixFormat))
|
2017-04-24 14:47:10 +02:00
|
|
|
default:
|
2018-01-03 19:12:40 +02:00
|
|
|
panic(fmt.Sprintf("unhandled command node: %T", x))
|
2017-06-24 21:00:10 +02:00
|
|
|
}
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
|
2018-01-03 19:12:40 +02:00
|
|
|
func elapsedString(d time.Duration, posix bool) string {
|
|
|
|
if posix {
|
|
|
|
return fmt.Sprintf("%.2f", d.Seconds())
|
|
|
|
}
|
2017-10-15 21:41:15 +02:00
|
|
|
min := int(d.Minutes())
|
|
|
|
sec := math.Remainder(d.Seconds(), 60.0)
|
|
|
|
return fmt.Sprintf("%dm%.3fs", min, sec)
|
|
|
|
}
|
|
|
|
|
2017-07-06 01:46:05 +02:00
|
|
|
func (r *Runner) stmts(sl syntax.StmtList) {
|
|
|
|
for _, stmt := range sl.Stmts {
|
2017-04-24 14:47:10 +02:00
|
|
|
r.stmt(stmt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) redir(rd *syntax.Redirect) (io.Closer, error) {
|
|
|
|
if rd.Hdoc != nil {
|
|
|
|
hdoc := r.loneWord(rd.Hdoc)
|
|
|
|
r.Stdin = strings.NewReader(hdoc)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
orig := &r.Stdout
|
|
|
|
if rd.N != nil {
|
|
|
|
switch rd.N.Value {
|
|
|
|
case "1":
|
|
|
|
case "2":
|
|
|
|
orig = &r.Stderr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arg := r.loneWord(rd.Word)
|
|
|
|
switch rd.Op {
|
|
|
|
case syntax.WordHdoc:
|
|
|
|
r.Stdin = strings.NewReader(arg + "\n")
|
|
|
|
return nil, nil
|
|
|
|
case syntax.DplOut:
|
|
|
|
switch arg {
|
|
|
|
case "1":
|
|
|
|
*orig = r.Stdout
|
|
|
|
case "2":
|
|
|
|
*orig = r.Stderr
|
|
|
|
}
|
|
|
|
return nil, nil
|
2017-11-19 22:26:37 +02:00
|
|
|
case syntax.RdrIn, syntax.RdrOut, syntax.AppOut,
|
|
|
|
syntax.RdrAll, syntax.AppAll:
|
|
|
|
// done further below
|
|
|
|
// case syntax.DplIn:
|
|
|
|
default:
|
2018-01-03 19:12:40 +02:00
|
|
|
panic(fmt.Sprintf("unhandled redirect op: %v", rd.Op))
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
mode := os.O_RDONLY
|
|
|
|
switch rd.Op {
|
|
|
|
case syntax.AppOut, syntax.AppAll:
|
|
|
|
mode = os.O_RDWR | os.O_CREATE | os.O_APPEND
|
|
|
|
case syntax.RdrOut, syntax.RdrAll:
|
|
|
|
mode = os.O_RDWR | os.O_CREATE | os.O_TRUNC
|
|
|
|
}
|
2017-10-15 21:41:15 +02:00
|
|
|
f, err := r.open(r.relPath(arg), mode, 0644, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
switch rd.Op {
|
|
|
|
case syntax.RdrIn:
|
|
|
|
r.Stdin = f
|
|
|
|
case syntax.RdrOut, syntax.AppOut:
|
|
|
|
*orig = f
|
|
|
|
case syntax.RdrAll, syntax.AppAll:
|
|
|
|
r.Stdout = f
|
|
|
|
r.Stderr = f
|
|
|
|
default:
|
2018-01-03 19:12:40 +02:00
|
|
|
panic(fmt.Sprintf("unhandled redirect op: %v", rd.Op))
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2017-07-06 01:46:05 +02:00
|
|
|
func (r *Runner) loopStmtsBroken(sl syntax.StmtList) bool {
|
2018-01-03 19:12:40 +02:00
|
|
|
oldInLoop := r.inLoop
|
2017-04-24 14:47:10 +02:00
|
|
|
r.inLoop = true
|
2018-01-03 19:12:40 +02:00
|
|
|
defer func() { r.inLoop = oldInLoop }()
|
2017-07-06 01:46:05 +02:00
|
|
|
for _, stmt := range sl.Stmts {
|
2017-04-24 14:47:10 +02:00
|
|
|
r.stmt(stmt)
|
|
|
|
if r.contnEnclosing > 0 {
|
|
|
|
r.contnEnclosing--
|
|
|
|
return r.contnEnclosing > 0
|
|
|
|
}
|
|
|
|
if r.breakEnclosing > 0 {
|
|
|
|
r.breakEnclosing--
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-11-19 22:26:37 +02:00
|
|
|
type returnCode uint8
|
|
|
|
|
|
|
|
func (returnCode) Error() string { return "returned" }
|
|
|
|
|
2018-01-03 19:12:40 +02:00
|
|
|
func (r *Runner) call(pos syntax.Pos, args []string) {
|
|
|
|
if r.stop() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name := args[0]
|
|
|
|
if body := r.Funcs[name]; body != nil {
|
2017-04-24 14:47:10 +02:00
|
|
|
// stack them to support nested func calls
|
2017-06-24 21:00:10 +02:00
|
|
|
oldParams := r.Params
|
2018-01-03 19:12:40 +02:00
|
|
|
r.Params = args[1:]
|
|
|
|
oldInFunc := r.inFunc
|
|
|
|
oldFuncVars := r.funcVars
|
|
|
|
r.funcVars = nil
|
|
|
|
r.inFunc = true
|
|
|
|
|
2017-04-24 14:47:10 +02:00
|
|
|
r.stmt(body)
|
2018-01-03 19:12:40 +02:00
|
|
|
|
2017-06-24 21:00:10 +02:00
|
|
|
r.Params = oldParams
|
2018-01-03 19:12:40 +02:00
|
|
|
r.funcVars = oldFuncVars
|
|
|
|
r.inFunc = oldInFunc
|
2017-11-19 22:26:37 +02:00
|
|
|
if code, ok := r.err.(returnCode); ok {
|
|
|
|
r.err = nil
|
|
|
|
r.exit = int(code)
|
|
|
|
}
|
2017-04-24 14:47:10 +02:00
|
|
|
return
|
|
|
|
}
|
2017-05-01 00:50:22 +02:00
|
|
|
if isBuiltin(name) {
|
2018-01-03 19:12:40 +02:00
|
|
|
r.exit = r.builtinCode(pos, name, args[1:])
|
2017-04-24 14:47:10 +02:00
|
|
|
return
|
|
|
|
}
|
2018-01-03 19:12:40 +02:00
|
|
|
r.exec(args)
|
2017-10-15 21:41:15 +02:00
|
|
|
}
|
|
|
|
|
2018-01-03 19:12:40 +02:00
|
|
|
func (r *Runner) exec(args []string) {
|
|
|
|
path := r.lookPath(args[0])
|
|
|
|
err := r.Exec(r.ctx(), path, args)
|
2017-04-24 14:47:10 +02:00
|
|
|
switch x := err.(type) {
|
2017-10-15 21:41:15 +02:00
|
|
|
case nil:
|
2017-04-24 14:47:10 +02:00
|
|
|
r.exit = 0
|
2017-10-15 21:41:15 +02:00
|
|
|
case ExitCode:
|
|
|
|
r.exit = int(x)
|
2018-01-03 19:12:40 +02:00
|
|
|
default: // module's custom fatal error
|
2017-10-15 21:41:15 +02:00
|
|
|
r.setErr(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) open(path string, flags int, mode os.FileMode, print bool) (io.ReadWriteCloser, error) {
|
|
|
|
f, err := r.Open(r.ctx(), path, flags, mode)
|
|
|
|
switch err.(type) {
|
|
|
|
case nil:
|
|
|
|
case *os.PathError:
|
|
|
|
if print {
|
|
|
|
r.errf("%v\n", err)
|
|
|
|
}
|
2018-01-03 19:12:40 +02:00
|
|
|
default: // module's custom fatal error
|
2017-10-15 21:41:15 +02:00
|
|
|
r.setErr(err)
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
2017-10-15 21:41:15 +02:00
|
|
|
return f, err
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
2018-01-03 19:12:40 +02:00
|
|
|
|
|
|
|
func (r *Runner) stat(name string) (os.FileInfo, error) {
|
|
|
|
return os.Stat(r.relPath(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) checkStat(file string) string {
|
|
|
|
d, err := r.stat(file)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
m := d.Mode()
|
|
|
|
if m.IsDir() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if runtime.GOOS != "windows" && m&0111 == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
|
|
|
func winHasExt(file string) bool {
|
|
|
|
i := strings.LastIndex(file, ".")
|
|
|
|
if i < 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return strings.LastIndexAny(file, `:\/`) < i
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) findExecutable(file string, exts []string) string {
|
|
|
|
if len(exts) == 0 {
|
|
|
|
// non-windows
|
|
|
|
return r.checkStat(file)
|
|
|
|
}
|
|
|
|
if winHasExt(file) && r.checkStat(file) != "" {
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
for _, e := range exts {
|
|
|
|
if f := file + e; r.checkStat(f) != "" {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// splitList is like filepath.SplitList, but always using the unix path
|
|
|
|
// list separator ':'. On Windows, it also makes sure not to split
|
|
|
|
// [A-Z]:[/\].
|
|
|
|
func splitList(path string) []string {
|
|
|
|
if path == "" {
|
|
|
|
return []string{""}
|
|
|
|
}
|
|
|
|
list := strings.Split(path, ":")
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
// join "C", "/foo" into "C:/foo"
|
|
|
|
var fixed []string
|
|
|
|
for i := 0; i < len(list); i++ {
|
|
|
|
s := list[i]
|
|
|
|
switch {
|
|
|
|
case len(s) != 1, s[0] < 'A', s[0] > 'Z':
|
|
|
|
// not a disk name
|
|
|
|
case i+1 >= len(list):
|
|
|
|
// last element
|
|
|
|
case strings.IndexAny(list[i+1], `/\`) != 0:
|
|
|
|
// next element doesn't start with / or \
|
|
|
|
default:
|
|
|
|
fixed = append(fixed, s+":"+list[i+1])
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fixed = append(fixed, s)
|
|
|
|
}
|
|
|
|
return fixed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) lookPath(file string) string {
|
|
|
|
pathList := splitList(r.getVar("PATH"))
|
|
|
|
chars := `/`
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
chars = `:\/`
|
|
|
|
// so that "foo" always tries "./foo"
|
|
|
|
pathList = append([]string{"."}, pathList...)
|
|
|
|
}
|
|
|
|
exts := r.pathExts()
|
|
|
|
if strings.ContainsAny(file, chars) {
|
|
|
|
return r.findExecutable(file, exts)
|
|
|
|
}
|
|
|
|
for _, dir := range pathList {
|
|
|
|
var path string
|
|
|
|
switch dir {
|
|
|
|
case "", ".":
|
|
|
|
// otherwise "foo" won't be "./foo"
|
|
|
|
path = "." + string(filepath.Separator) + file
|
|
|
|
default:
|
|
|
|
path = filepath.Join(dir, file)
|
|
|
|
}
|
|
|
|
if f := r.findExecutable(path, exts); f != "" {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) pathExts() []string {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pathext := r.getVar("PATHEXT")
|
|
|
|
if pathext == "" {
|
|
|
|
return []string{".com", ".exe", ".bat", ".cmd"}
|
|
|
|
}
|
|
|
|
var exts []string
|
|
|
|
for _, e := range strings.Split(strings.ToLower(pathext), `;`) {
|
|
|
|
if e == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if e[0] != '.' {
|
|
|
|
e = "." + e
|
|
|
|
}
|
|
|
|
exts = append(exts, e)
|
|
|
|
}
|
|
|
|
return exts
|
|
|
|
}
|