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/exec"
|
|
|
|
"os/user"
|
|
|
|
"path"
|
2017-06-14 19:00:36 +02:00
|
|
|
"path/filepath"
|
2017-04-24 14:47:10 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
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.
|
|
|
|
Env []string
|
|
|
|
|
|
|
|
// envMap is just Env as a map, to simplify and speed up its use
|
|
|
|
envMap map[string]string
|
|
|
|
|
|
|
|
// 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-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
|
|
|
|
vars map[string]varValue
|
|
|
|
funcs map[string]*syntax.Stmt
|
|
|
|
|
|
|
|
// like vars, but local to a cmd i.e. "foo=bar prog args..."
|
|
|
|
cmdVars map[string]varValue
|
|
|
|
|
|
|
|
// >0 to break or continue out of N enclosing loops
|
|
|
|
breakEnclosing, contnEnclosing int
|
|
|
|
|
|
|
|
inLoop bool
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
stopOnCmdErr bool // set -e
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// varValue can hold a string, an indexed array ([]string) or an
|
|
|
|
// associative array (map[string]string)
|
|
|
|
// TODO: implement associative arrays
|
|
|
|
type varValue interface{}
|
|
|
|
|
|
|
|
func varStr(v varValue) string {
|
|
|
|
switch x := v.(type) {
|
|
|
|
case string:
|
|
|
|
return x
|
|
|
|
case []string:
|
|
|
|
if len(x) > 0 {
|
|
|
|
return x[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) varInd(v varValue, e syntax.ArithmExpr) string {
|
|
|
|
switch x := v.(type) {
|
|
|
|
case string:
|
|
|
|
i := r.arithm(e)
|
|
|
|
if i == 0 {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
case []string:
|
2017-05-27 16:17:49 +02:00
|
|
|
if w, ok := e.(*syntax.Word); ok {
|
|
|
|
if lit, ok := w.Parts[0].(*syntax.Lit); ok {
|
|
|
|
switch lit.Value {
|
|
|
|
case "@", "*":
|
|
|
|
return strings.Join(x, " ")
|
|
|
|
}
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
i := r.arithm(e)
|
|
|
|
if len(x) > 0 {
|
|
|
|
return x[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExitCode uint8
|
|
|
|
|
|
|
|
func (e ExitCode) Error() string { return fmt.Sprintf("exit status %d", e) }
|
|
|
|
|
|
|
|
type RunError struct {
|
2017-07-06 01:46:05 +02:00
|
|
|
Filename string
|
|
|
|
syntax.Pos
|
2017-04-24 14:47:10 +02:00
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e RunError) Error() string {
|
2017-07-06 01:46:05 +02:00
|
|
|
if e.Filename == "" {
|
|
|
|
return fmt.Sprintf("%s: %s", e.Pos.String(), e.Text)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s:%s: %s", e.Filename, e.Pos.String(), e.Text)
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) runErr(pos syntax.Pos, format string, a ...interface{}) {
|
|
|
|
if r.err == nil {
|
|
|
|
r.err = RunError{
|
2017-08-05 19:13:35 +02:00
|
|
|
Filename: r.filename,
|
|
|
|
Pos: pos,
|
|
|
|
Text: fmt.Sprintf(format, a...),
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) lastExit() {
|
|
|
|
if r.err == nil {
|
|
|
|
r.err = ExitCode(r.exit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) setVar(name string, val varValue) {
|
|
|
|
if r.vars == nil {
|
|
|
|
r.vars = make(map[string]varValue, 4)
|
|
|
|
}
|
|
|
|
r.vars[name] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) lookupVar(name string) (varValue, bool) {
|
|
|
|
switch name {
|
|
|
|
case "PWD":
|
|
|
|
return r.Dir, true
|
|
|
|
case "HOME":
|
|
|
|
u, _ := user.Current()
|
|
|
|
return u.HomeDir, true
|
|
|
|
}
|
|
|
|
if val, e := r.cmdVars[name]; e {
|
|
|
|
return val, true
|
|
|
|
}
|
|
|
|
if val, e := r.vars[name]; e {
|
|
|
|
return val, true
|
|
|
|
}
|
|
|
|
str, e := r.envMap[name]
|
|
|
|
return str, e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) getVar(name string) string {
|
|
|
|
val, _ := r.lookupVar(name)
|
|
|
|
return varStr(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) delVar(name string) {
|
|
|
|
delete(r.vars, name)
|
|
|
|
delete(r.envMap, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) setFunc(name string, body *syntax.Stmt) {
|
|
|
|
if r.funcs == nil {
|
|
|
|
r.funcs = make(map[string]*syntax.Stmt, 4)
|
|
|
|
}
|
|
|
|
r.funcs[name] = body
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
opts:
|
|
|
|
for len(args) > 0 {
|
|
|
|
opt := args[0]
|
|
|
|
if opt == "" || (opt[0] != '-' && opt[0] != '+') {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
enable := opt[0] == '-'
|
|
|
|
switch opt[1:] {
|
|
|
|
case "-":
|
|
|
|
args = args[1:]
|
|
|
|
break opts
|
|
|
|
case "e":
|
|
|
|
r.stopOnCmdErr = enable
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid option: %q", opt)
|
|
|
|
}
|
|
|
|
args = args[1:]
|
|
|
|
}
|
|
|
|
return args, nil
|
|
|
|
}
|
|
|
|
|
2017-08-05 19:13:35 +02:00
|
|
|
func (r *Runner) Reset() error {
|
|
|
|
// reset the internal state
|
|
|
|
*r = Runner{
|
|
|
|
Env: r.Env,
|
|
|
|
Dir: r.Dir,
|
|
|
|
Params: r.Params,
|
|
|
|
Context: r.Context,
|
|
|
|
Stdin: r.Stdin,
|
|
|
|
Stdout: r.Stdout,
|
|
|
|
Stderr: r.Stderr,
|
|
|
|
}
|
2017-04-24 14:47:10 +02:00
|
|
|
if r.Context == nil {
|
|
|
|
r.Context = context.Background()
|
|
|
|
}
|
|
|
|
if r.Env == nil {
|
|
|
|
r.Env = os.Environ()
|
|
|
|
}
|
|
|
|
r.envMap = make(map[string]string, len(r.Env))
|
|
|
|
for _, kv := range r.Env {
|
|
|
|
i := strings.IndexByte(kv, '=')
|
|
|
|
if i < 0 {
|
|
|
|
return fmt.Errorf("env not in the form key=value: %q", kv)
|
|
|
|
}
|
|
|
|
name, val := kv[:i], kv[i+1:]
|
|
|
|
r.envMap[name] = val
|
|
|
|
}
|
|
|
|
if r.Dir == "" {
|
|
|
|
dir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get current dir: %v", err)
|
|
|
|
}
|
|
|
|
r.Dir = dir
|
|
|
|
}
|
2017-08-05 19:13:35 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
2017-07-31 00:11:34 +02:00
|
|
|
func (r *Runner) expand(format string, onlyChars bool, args ...string) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
esc, fmt := false, false
|
|
|
|
for _, c := range format {
|
|
|
|
if esc {
|
|
|
|
esc = false
|
|
|
|
switch c {
|
|
|
|
case 'n':
|
|
|
|
buf.WriteRune('\n')
|
|
|
|
case 'r':
|
|
|
|
buf.WriteRune('\r')
|
|
|
|
case 't':
|
|
|
|
buf.WriteRune('\t')
|
|
|
|
case '\\':
|
|
|
|
buf.WriteRune('\\')
|
|
|
|
default:
|
|
|
|
buf.WriteRune('\\')
|
|
|
|
buf.WriteRune(c)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fmt {
|
|
|
|
fmt = false
|
|
|
|
arg := ""
|
2017-08-05 19:13:35 +02:00
|
|
|
n := 0
|
2017-07-31 00:11:34 +02:00
|
|
|
if len(args) > 0 {
|
|
|
|
arg, args = args[0], args[1:]
|
2017-08-05 19:13:35 +02:00
|
|
|
i, _ := strconv.ParseInt(arg, 0, 0)
|
|
|
|
n = int(i)
|
2017-07-31 00:11:34 +02:00
|
|
|
}
|
|
|
|
switch c {
|
|
|
|
case 's':
|
|
|
|
buf.WriteString(arg)
|
2017-08-05 19:13:35 +02:00
|
|
|
case 'c':
|
|
|
|
var b byte
|
|
|
|
if len(arg) > 0 {
|
|
|
|
b = arg[0]
|
|
|
|
}
|
|
|
|
buf.WriteByte(b)
|
|
|
|
case 'd', 'i':
|
2017-07-31 00:11:34 +02:00
|
|
|
buf.WriteString(strconv.Itoa(n))
|
2017-08-05 19:13:35 +02:00
|
|
|
case 'u':
|
|
|
|
buf.WriteString(strconv.FormatUint(uint64(n), 10))
|
|
|
|
case 'o':
|
|
|
|
buf.WriteString(strconv.FormatUint(uint64(n), 8))
|
|
|
|
case 'x':
|
|
|
|
buf.WriteString(strconv.FormatUint(uint64(n), 16))
|
2017-07-31 00:11:34 +02:00
|
|
|
default:
|
|
|
|
r.runErr(syntax.Pos{}, "unhandled format char: %c", c)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if c == '\\' {
|
|
|
|
esc = true
|
|
|
|
} else if !onlyChars && c == '%' {
|
|
|
|
fmt = true
|
|
|
|
} else {
|
|
|
|
buf.WriteRune(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2017-06-14 19:00:36 +02:00
|
|
|
func fieldJoin(parts []fieldPart) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, part := range parts {
|
|
|
|
buf.WriteString(part.val)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2017-06-16 19:59:12 +02:00
|
|
|
func escapedGlob(parts []fieldPart) (escaped string, glob bool) {
|
2017-06-14 19:00:36 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, part := range parts {
|
|
|
|
for _, r := range part.val {
|
|
|
|
switch r {
|
|
|
|
case '*', '?', '\\', '[':
|
2017-06-16 19:59:12 +02:00
|
|
|
if part.quoted {
|
|
|
|
buf.WriteByte('\\')
|
|
|
|
} else {
|
|
|
|
glob = true
|
|
|
|
}
|
2017-06-14 19:00:36 +02:00
|
|
|
}
|
|
|
|
buf.WriteRune(r)
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 19:59:12 +02:00
|
|
|
return buf.String(), glob
|
2017-06-14 19:00:36 +02:00
|
|
|
}
|
|
|
|
|
2017-04-24 14:47:10 +02:00
|
|
|
func (r *Runner) fields(words []*syntax.Word) []string {
|
|
|
|
fields := make([]string, 0, len(words))
|
2017-06-16 19:59:12 +02:00
|
|
|
baseDir, _ := escapedGlob([]fieldPart{{val: r.Dir}})
|
2017-04-24 14:47:10 +02:00
|
|
|
for _, word := range words {
|
2017-06-14 19:00:36 +02:00
|
|
|
for _, field := range r.wordFields(word.Parts, false) {
|
2017-06-16 19:59:12 +02:00
|
|
|
path, glob := escapedGlob(field)
|
|
|
|
var matches []string
|
|
|
|
abs := filepath.IsAbs(path)
|
|
|
|
if glob {
|
|
|
|
if !abs {
|
|
|
|
path = filepath.Join(baseDir, path)
|
|
|
|
}
|
|
|
|
matches, _ = filepath.Glob(path)
|
|
|
|
}
|
|
|
|
if len(matches) == 0 {
|
2017-06-14 19:00:36 +02:00
|
|
|
fields = append(fields, fieldJoin(field))
|
2017-06-16 19:59:12 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, match := range matches {
|
|
|
|
if !abs {
|
|
|
|
match, _ = filepath.Rel(baseDir, match)
|
|
|
|
}
|
|
|
|
fields = append(fields, match)
|
2017-06-14 19:00:36 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
return fields
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) loneWord(word *syntax.Word) string {
|
2017-05-27 16:17:49 +02:00
|
|
|
if word == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2017-06-14 19:00:36 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, field := range r.wordFields(word.Parts, false) {
|
|
|
|
for _, part := range field {
|
|
|
|
buf.WriteString(part.val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) stop() bool {
|
|
|
|
if r.err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err := r.Context.Err(); err != nil {
|
|
|
|
r.err = err
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) stmt(st *syntax.Stmt) {
|
|
|
|
if r.stop() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if st.Background {
|
|
|
|
r.bgShells.Add(1)
|
|
|
|
r2 := *r
|
|
|
|
r2.bgShells = sync.WaitGroup{}
|
|
|
|
go func() {
|
|
|
|
r2.stmtSync(st)
|
|
|
|
r.bgShells.Done()
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
r.stmtSync(st)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 19:49:27 +02:00
|
|
|
func (r *Runner) assignValue(as *syntax.Assign) varValue {
|
2017-05-27 16:17:49 +02:00
|
|
|
prev, _ := r.lookupVar(as.Name.Value)
|
2017-05-17 19:49:27 +02:00
|
|
|
if as.Value != nil {
|
2017-05-27 16:17:49 +02:00
|
|
|
s := r.loneWord(as.Value)
|
|
|
|
if !as.Append || prev == nil {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
switch x := prev.(type) {
|
|
|
|
case string:
|
|
|
|
return x + s
|
|
|
|
case []string:
|
|
|
|
if len(x) == 0 {
|
|
|
|
return []string{s}
|
|
|
|
}
|
|
|
|
x[0] += s
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return s
|
2017-05-17 19:49:27 +02:00
|
|
|
}
|
|
|
|
if as.Array != nil {
|
2017-05-27 16:17:49 +02:00
|
|
|
strs := make([]string, len(as.Array.Elems))
|
|
|
|
for i, elem := range as.Array.Elems {
|
|
|
|
strs[i] = r.loneWord(elem.Value)
|
|
|
|
}
|
|
|
|
if !as.Append || prev == nil {
|
|
|
|
return strs
|
|
|
|
}
|
|
|
|
switch x := prev.(type) {
|
|
|
|
case string:
|
|
|
|
return append([]string{x}, strs...)
|
|
|
|
case []string:
|
|
|
|
return append(x, strs...)
|
2017-05-17 19:49:27 +02:00
|
|
|
}
|
|
|
|
return strs
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
2017-05-17 19:49:27 +02:00
|
|
|
return nil
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
r.Stdin, r.Stdout, r.Stderr = oldIn, oldOut, oldErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func oneIf(b bool) int {
|
|
|
|
if b {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
r2 := *r
|
2017-07-06 01:46:05 +02:00
|
|
|
r2.stmts(x.StmtList)
|
2017-04-24 14:47:10 +02:00
|
|
|
r.exit = r2.exit
|
|
|
|
case *syntax.CallExpr:
|
2017-07-31 00:11:34 +02:00
|
|
|
if len(x.Args) == 0 {
|
|
|
|
for _, as := range x.Assigns {
|
|
|
|
r.setVar(as.Name.Value, r.assignValue(as))
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
oldVars := r.cmdVars
|
|
|
|
if r.cmdVars == nil {
|
|
|
|
r.cmdVars = make(map[string]varValue, len(x.Assigns))
|
|
|
|
}
|
|
|
|
for _, as := range x.Assigns {
|
|
|
|
r.cmdVars[as.Name.Value] = r.assignValue(as)
|
|
|
|
}
|
2017-04-24 14:47:10 +02:00
|
|
|
fields := r.fields(x.Args)
|
|
|
|
r.call(x.Args[0].Pos(), fields[0], fields[1:])
|
2017-07-31 00:11:34 +02:00
|
|
|
r.cmdVars = oldVars
|
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()
|
|
|
|
r2 := *r
|
|
|
|
r2.Stdin = r.Stdin
|
|
|
|
r2.Stdout = pw
|
|
|
|
if x.Op == syntax.PipeAll {
|
|
|
|
r2.Stderr = pw
|
|
|
|
} else {
|
|
|
|
r2.Stderr = r.Stderr
|
|
|
|
}
|
|
|
|
r.Stdin = pr
|
|
|
|
go func() {
|
|
|
|
r2.stmt(x.X)
|
|
|
|
pw.Close()
|
|
|
|
}()
|
|
|
|
r.stmt(x.Y)
|
|
|
|
pr.Close()
|
|
|
|
}
|
|
|
|
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)
|
2017-04-24 14:47:10 +02:00
|
|
|
return
|
|
|
|
}
|
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:
|
|
|
|
for r.err == nil {
|
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
|
2017-05-27 16:17:49 +02:00
|
|
|
for _, field := range r.fields(y.Items) {
|
2017-04-24 14:47:10 +02:00
|
|
|
r.setVar(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:
|
|
|
|
if r.arithm(x.X) == 0 {
|
|
|
|
r.exit = 1
|
|
|
|
}
|
|
|
|
case *syntax.LetClause:
|
|
|
|
var val int
|
|
|
|
for _, expr := range x.Exprs {
|
|
|
|
val = r.arithm(expr)
|
|
|
|
}
|
|
|
|
if val == 0 {
|
|
|
|
r.exit = 1
|
|
|
|
}
|
|
|
|
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 {
|
2017-06-14 19:00:36 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, field := range r.wordFields(word.Parts, false) {
|
2017-06-16 19:59:12 +02:00
|
|
|
escaped, _ := escapedGlob(field)
|
|
|
|
buf.WriteString(escaped)
|
2017-06-14 19:00:36 +02:00
|
|
|
}
|
|
|
|
if match(buf.String(), 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:
|
|
|
|
if r.bashTest(x.X) == "" && r.exit == 0 {
|
|
|
|
r.exit = 1
|
|
|
|
}
|
2017-05-27 16:17:49 +02:00
|
|
|
case *syntax.DeclClause:
|
|
|
|
if len(x.Opts) > 0 {
|
|
|
|
r.runErr(cm.Pos(), "unhandled declare opts")
|
|
|
|
}
|
|
|
|
for _, as := range x.Assigns {
|
|
|
|
r.setVar(as.Name.Value, r.assignValue(as))
|
|
|
|
}
|
2017-09-07 19:02:52 +02:00
|
|
|
case *syntax.TimeClause:
|
|
|
|
start := time.Now()
|
|
|
|
if x.Stmt != nil {
|
|
|
|
r.stmt(x.Stmt)
|
|
|
|
}
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
r.outf("\n")
|
|
|
|
min := int(elapsed.Minutes())
|
|
|
|
sec := math.Remainder(elapsed.Seconds(), 60.0)
|
|
|
|
r.outf("real\t%dm%.3fs\n", min, sec)
|
|
|
|
// TODO: can we do these?
|
|
|
|
r.outf("user\t0m0.000s\n")
|
|
|
|
r.outf("sys\t0m0.000s\n")
|
2017-04-24 14:47:10 +02:00
|
|
|
default:
|
2017-05-17 19:49:27 +02:00
|
|
|
r.runErr(cm.Pos(), "unhandled command node: %T", x)
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
2017-06-24 21:00:10 +02:00
|
|
|
if r.exit != 0 && r.stopOnCmdErr {
|
|
|
|
r.lastExit()
|
|
|
|
}
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 19:49:27 +02:00
|
|
|
func match(pattern, name string) bool {
|
|
|
|
matched, _ := path.Match(pattern, name)
|
|
|
|
return matched
|
|
|
|
}
|
|
|
|
|
2017-04-24 14:47:10 +02:00
|
|
|
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
|
|
|
|
case syntax.DplIn:
|
2017-05-17 19:49:27 +02:00
|
|
|
r.runErr(rd.Pos(), "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-07-31 00:11:34 +02:00
|
|
|
var f io.ReadWriteCloser
|
|
|
|
switch arg {
|
|
|
|
case "/dev/null":
|
|
|
|
f = devNull{}
|
|
|
|
default:
|
|
|
|
var err error
|
|
|
|
f, err = os.OpenFile(r.relPath(arg), mode, 0644)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: print to stderr?
|
|
|
|
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:
|
2017-05-17 19:49:27 +02:00
|
|
|
r.runErr(rd.Pos(), "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 {
|
2017-04-24 14:47:10 +02:00
|
|
|
r.inLoop = true
|
|
|
|
defer func() { r.inLoop = false }()
|
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-06-14 19:00:36 +02:00
|
|
|
type fieldPart struct {
|
|
|
|
val string
|
|
|
|
quoted bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) wordFields(wps []syntax.WordPart, quoted bool) [][]fieldPart {
|
|
|
|
var fields [][]fieldPart
|
|
|
|
var curField []fieldPart
|
2017-06-04 21:06:04 +02:00
|
|
|
allowEmpty := false
|
2017-04-24 14:47:10 +02:00
|
|
|
flush := func() {
|
2017-06-14 19:00:36 +02:00
|
|
|
if len(curField) == 0 {
|
2017-04-24 14:47:10 +02:00
|
|
|
return
|
|
|
|
}
|
2017-06-14 19:00:36 +02:00
|
|
|
fields = append(fields, curField)
|
|
|
|
curField = nil
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
splitAdd := func(val string) {
|
|
|
|
// TODO: use IFS
|
|
|
|
for i, field := range strings.Fields(val) {
|
|
|
|
if i > 0 {
|
|
|
|
flush()
|
|
|
|
}
|
2017-06-14 19:00:36 +02:00
|
|
|
curField = append(curField, fieldPart{val: field})
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-01 00:50:22 +02:00
|
|
|
for i, wp := range wps {
|
2017-04-24 14:47:10 +02:00
|
|
|
switch x := wp.(type) {
|
|
|
|
case *syntax.Lit:
|
2017-05-01 00:50:22 +02:00
|
|
|
s := x.Value
|
|
|
|
if i > 0 || len(s) == 0 || s[0] != '~' {
|
|
|
|
} else if len(s) < 2 || s[1] == '/' {
|
|
|
|
// TODO: ~someuser
|
|
|
|
s = r.getVar("HOME") + s[1:]
|
|
|
|
}
|
2017-06-14 19:00:36 +02:00
|
|
|
curField = append(curField, fieldPart{val: s})
|
2017-04-24 14:47:10 +02:00
|
|
|
case *syntax.SglQuoted:
|
2017-06-04 21:06:04 +02:00
|
|
|
allowEmpty = true
|
2017-07-31 00:11:34 +02:00
|
|
|
fp := fieldPart{quoted: true, val: x.Value}
|
|
|
|
if x.Dollar {
|
|
|
|
fp.val = r.expand(fp.val, true)
|
|
|
|
}
|
|
|
|
curField = append(curField, fp)
|
2017-04-24 14:47:10 +02:00
|
|
|
case *syntax.DblQuoted:
|
2017-06-04 21:06:04 +02:00
|
|
|
allowEmpty = true
|
2017-04-24 14:47:10 +02:00
|
|
|
if len(x.Parts) == 1 {
|
2017-06-04 21:06:04 +02:00
|
|
|
pe, _ := x.Parts[0].(*syntax.ParamExp)
|
|
|
|
if elems := r.quotedElems(pe); elems != nil {
|
|
|
|
for i, elem := range elems {
|
2017-04-24 14:47:10 +02:00
|
|
|
if i > 0 {
|
|
|
|
flush()
|
|
|
|
}
|
2017-06-14 19:00:36 +02:00
|
|
|
curField = append(curField, fieldPart{
|
|
|
|
quoted: true,
|
|
|
|
val: elem,
|
|
|
|
})
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2017-06-14 19:00:36 +02:00
|
|
|
for _, field := range r.wordFields(x.Parts, true) {
|
|
|
|
for _, part := range field {
|
|
|
|
curField = append(curField, fieldPart{
|
|
|
|
quoted: true,
|
|
|
|
val: part.val,
|
|
|
|
})
|
|
|
|
}
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
case *syntax.ParamExp:
|
|
|
|
val := r.paramExp(x)
|
|
|
|
if quoted {
|
2017-06-14 19:00:36 +02:00
|
|
|
curField = append(curField, fieldPart{val: val})
|
2017-04-24 14:47:10 +02:00
|
|
|
} else {
|
|
|
|
splitAdd(val)
|
|
|
|
}
|
|
|
|
case *syntax.CmdSubst:
|
|
|
|
r2 := *r
|
2017-06-04 21:06:04 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
r2.Stdout = &buf
|
2017-07-06 01:46:05 +02:00
|
|
|
r2.stmts(x.StmtList)
|
2017-06-04 21:06:04 +02:00
|
|
|
val := strings.TrimRight(buf.String(), "\n")
|
2017-04-24 14:47:10 +02:00
|
|
|
if quoted {
|
2017-06-14 19:00:36 +02:00
|
|
|
curField = append(curField, fieldPart{val: val})
|
2017-04-24 14:47:10 +02:00
|
|
|
} else {
|
|
|
|
splitAdd(val)
|
|
|
|
}
|
|
|
|
case *syntax.ArithmExp:
|
2017-06-14 19:00:36 +02:00
|
|
|
curField = append(curField, fieldPart{
|
|
|
|
val: strconv.Itoa(r.arithm(x.X)),
|
|
|
|
})
|
2017-04-24 14:47:10 +02:00
|
|
|
default:
|
2017-05-17 19:49:27 +02:00
|
|
|
r.runErr(wp.Pos(), "unhandled word part: %T", x)
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
flush()
|
2017-06-14 19:00:36 +02:00
|
|
|
if allowEmpty && len(fields) == 0 {
|
|
|
|
fields = append(fields, []fieldPart{{}})
|
2017-06-04 21:06:04 +02:00
|
|
|
}
|
2017-06-14 19:00:36 +02:00
|
|
|
return fields
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) call(pos syntax.Pos, name string, args []string) {
|
|
|
|
if body := r.funcs[name]; body != nil {
|
|
|
|
// stack them to support nested func calls
|
2017-06-24 21:00:10 +02:00
|
|
|
oldParams := r.Params
|
|
|
|
r.Params = args
|
2017-04-24 14:47:10 +02:00
|
|
|
r.stmt(body)
|
2017-06-24 21:00:10 +02:00
|
|
|
r.Params = oldParams
|
2017-04-24 14:47:10 +02:00
|
|
|
return
|
|
|
|
}
|
2017-05-01 00:50:22 +02:00
|
|
|
if isBuiltin(name) {
|
2017-05-17 19:49:27 +02:00
|
|
|
r.exit = r.builtinCode(pos, name, args)
|
2017-04-24 14:47:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
cmd := exec.CommandContext(r.Context, name, args...)
|
|
|
|
cmd.Env = r.Env
|
|
|
|
for name, val := range r.cmdVars {
|
|
|
|
cmd.Env = append(cmd.Env, name+"="+varStr(val))
|
|
|
|
}
|
|
|
|
cmd.Dir = r.Dir
|
|
|
|
cmd.Stdin = r.Stdin
|
|
|
|
cmd.Stdout = r.Stdout
|
|
|
|
cmd.Stderr = r.Stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
switch x := err.(type) {
|
|
|
|
case *exec.ExitError:
|
|
|
|
// started, but errored - default to 1 if OS
|
|
|
|
// doesn't have exit statuses
|
2017-05-17 19:49:27 +02:00
|
|
|
r.exit = 1
|
2017-04-24 14:47:10 +02:00
|
|
|
if status, ok := x.Sys().(syscall.WaitStatus); ok {
|
|
|
|
r.exit = status.ExitStatus()
|
|
|
|
}
|
|
|
|
case *exec.Error:
|
|
|
|
// did not start
|
|
|
|
// TODO: can this be anything other than
|
|
|
|
// "command not found"?
|
|
|
|
r.exit = 127
|
|
|
|
// TODO: print something?
|
|
|
|
default:
|
|
|
|
r.exit = 0
|
|
|
|
}
|
|
|
|
}
|