2017-04-24 14:47:10 +02:00
|
|
|
// Copyright (c) 2017, Daniel Martí <mvdan@mvdan.cc>
|
|
|
|
// See LICENSE for licensing information
|
|
|
|
|
|
|
|
package interp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2017-05-01 00:50:22 +02:00
|
|
|
"os/exec"
|
2017-04-24 14:47:10 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2017-05-17 19:49:27 +02:00
|
|
|
"strings"
|
2017-04-24 14:47:10 +02:00
|
|
|
|
|
|
|
"github.com/mvdan/sh/syntax"
|
|
|
|
)
|
|
|
|
|
2017-05-01 00:50:22 +02:00
|
|
|
func isBuiltin(name string) bool {
|
|
|
|
switch name {
|
|
|
|
case "true", ":", "false", "exit", "set", "shift", "unset",
|
|
|
|
"echo", "printf", "break", "continue", "pwd", "cd",
|
|
|
|
"wait", "builtin", "trap", "type", "source", "command",
|
|
|
|
"pushd", "popd", "umask", "alias", "unalias", "fg", "bg",
|
2017-06-04 21:06:04 +02:00
|
|
|
"getopts", "eval", "test", "[":
|
2017-05-01 00:50:22 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-17 19:49:27 +02:00
|
|
|
func (r *Runner) builtinCode(pos syntax.Pos, name string, args []string) int {
|
2017-04-24 14:47:10 +02:00
|
|
|
switch name {
|
|
|
|
case "true", ":":
|
|
|
|
case "false":
|
2017-05-17 19:49:27 +02:00
|
|
|
return 1
|
2017-04-24 14:47:10 +02:00
|
|
|
case "exit":
|
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
if n, err := strconv.Atoi(args[0]); err != nil {
|
|
|
|
r.runErr(pos, "invalid exit code: %q", args[0])
|
|
|
|
} else {
|
2017-05-17 19:49:27 +02:00
|
|
|
r.exit = n
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
r.runErr(pos, "exit cannot take multiple arguments")
|
|
|
|
}
|
2017-05-17 19:49:27 +02:00
|
|
|
r.lastExit()
|
|
|
|
return r.exit
|
2017-04-24 14:47:10 +02:00
|
|
|
case "set":
|
|
|
|
r.args = args
|
|
|
|
case "shift":
|
|
|
|
n := 1
|
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
if n2, err := strconv.Atoi(args[0]); err == nil {
|
|
|
|
n = n2
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
r.errf("usage: shift [n]\n")
|
2017-05-17 19:49:27 +02:00
|
|
|
return 2
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
if len(r.args) < n {
|
|
|
|
n = len(r.args)
|
|
|
|
}
|
|
|
|
r.args = r.args[n:]
|
|
|
|
case "unset":
|
|
|
|
for _, arg := range args {
|
|
|
|
r.delVar(arg)
|
|
|
|
}
|
|
|
|
case "echo":
|
|
|
|
newline := true
|
|
|
|
opts:
|
|
|
|
for len(args) > 0 {
|
|
|
|
switch args[0] {
|
|
|
|
case "-n":
|
|
|
|
newline = false
|
|
|
|
case "-e", "-E":
|
|
|
|
// TODO: what should be our default?
|
|
|
|
// exactly what is the difference in
|
|
|
|
// what we write?
|
|
|
|
default:
|
|
|
|
break opts
|
|
|
|
}
|
|
|
|
args = args[1:]
|
|
|
|
}
|
|
|
|
for i, arg := range args {
|
|
|
|
if i > 0 {
|
|
|
|
r.outf(" ")
|
|
|
|
}
|
|
|
|
r.outf("%s", arg)
|
|
|
|
}
|
|
|
|
if newline {
|
|
|
|
r.outf("\n")
|
|
|
|
}
|
|
|
|
case "printf":
|
|
|
|
if len(args) == 0 {
|
|
|
|
r.errf("usage: printf format [arguments]\n")
|
2017-05-17 19:49:27 +02:00
|
|
|
return 2
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
var a []interface{}
|
|
|
|
for _, arg := range args[1:] {
|
|
|
|
a = append(a, arg)
|
|
|
|
}
|
|
|
|
r.outf(args[0], a...)
|
|
|
|
case "break":
|
|
|
|
if !r.inLoop {
|
|
|
|
r.errf("break is only useful in a loop")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
r.breakEnclosing = 1
|
|
|
|
case 1:
|
|
|
|
if n, err := strconv.Atoi(args[0]); err == nil {
|
|
|
|
r.breakEnclosing = n
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
r.errf("usage: break [n]\n")
|
2017-05-17 19:49:27 +02:00
|
|
|
return 2
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
case "continue":
|
|
|
|
if !r.inLoop {
|
|
|
|
r.errf("continue is only useful in a loop")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
r.contnEnclosing = 1
|
|
|
|
case 1:
|
|
|
|
if n, err := strconv.Atoi(args[0]); err == nil {
|
|
|
|
r.contnEnclosing = n
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
r.errf("usage: continue [n]\n")
|
2017-05-17 19:49:27 +02:00
|
|
|
return 2
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
case "pwd":
|
|
|
|
r.outf("%s\n", r.getVar("PWD"))
|
|
|
|
case "cd":
|
|
|
|
if len(args) > 1 {
|
|
|
|
r.errf("usage: cd [dir]\n")
|
2017-05-17 19:49:27 +02:00
|
|
|
return 2
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
var dir string
|
|
|
|
if len(args) == 0 {
|
|
|
|
dir = r.getVar("HOME")
|
|
|
|
} else {
|
|
|
|
dir = args[0]
|
|
|
|
}
|
|
|
|
if !filepath.IsAbs(dir) {
|
|
|
|
dir = filepath.Join(r.Dir, dir)
|
|
|
|
}
|
|
|
|
_, err := os.Stat(dir)
|
|
|
|
if err != nil {
|
2017-05-17 19:49:27 +02:00
|
|
|
return 1
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
|
|
|
r.Dir = dir
|
|
|
|
case "wait":
|
|
|
|
if len(args) > 0 {
|
2017-05-17 19:49:27 +02:00
|
|
|
r.runErr(pos, "wait with args not handled yet")
|
2017-04-24 14:47:10 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
r.bgShells.Wait()
|
|
|
|
case "builtin":
|
|
|
|
if len(args) < 1 {
|
|
|
|
break
|
|
|
|
}
|
2017-05-01 00:50:22 +02:00
|
|
|
if !isBuiltin(args[0]) {
|
2017-05-17 19:49:27 +02:00
|
|
|
return 1
|
2017-05-01 00:50:22 +02:00
|
|
|
}
|
2017-05-17 19:49:27 +02:00
|
|
|
return r.builtinCode(pos, args[0], args[1:])
|
2017-05-01 00:50:22 +02:00
|
|
|
case "type":
|
2017-05-17 19:49:27 +02:00
|
|
|
anyNotFound := false
|
2017-05-01 00:50:22 +02:00
|
|
|
for _, arg := range args {
|
2017-05-17 19:49:27 +02:00
|
|
|
if _, ok := r.funcs[arg]; ok {
|
|
|
|
r.outf("%s is a function\n", arg)
|
|
|
|
continue
|
|
|
|
}
|
2017-05-01 00:50:22 +02:00
|
|
|
if isBuiltin(arg) {
|
|
|
|
r.outf("%s is a shell builtin\n", arg)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if path, err := exec.LookPath(arg); err == nil {
|
|
|
|
r.outf("%s is %s\n", arg, path)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r.errf("type: %s: not found\n", arg)
|
2017-05-17 19:49:27 +02:00
|
|
|
anyNotFound = true
|
|
|
|
}
|
|
|
|
if anyNotFound {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
case "eval":
|
|
|
|
src := strings.Join(args, " ")
|
|
|
|
p := syntax.NewParser()
|
|
|
|
file, err := p.Parse(strings.NewReader(src), "")
|
|
|
|
if err != nil {
|
|
|
|
r.errf("eval: %v\n", err)
|
|
|
|
return 1
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
2017-05-17 19:49:27 +02:00
|
|
|
r2 := *r
|
|
|
|
r2.File = file
|
|
|
|
r2.Run()
|
|
|
|
return r2.exit
|
2017-06-04 21:06:04 +02:00
|
|
|
case "[":
|
|
|
|
if len(args) == 0 || args[len(args)-1] != "]" {
|
|
|
|
r.runErr(pos, "[: missing matching ]")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
args = args[:len(args)-1]
|
|
|
|
fallthrough
|
|
|
|
case "test":
|
|
|
|
p := testParser{
|
|
|
|
rem: args,
|
|
|
|
err: func(format string, a ...interface{}) {
|
|
|
|
r.runErr(pos, format, a...)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
expr := p.classicTest("[", false)
|
|
|
|
return oneIf(r.bashTest(expr) == "")
|
2017-05-01 00:50:22 +02:00
|
|
|
case "trap", "source", "command", "pushd", "popd",
|
2017-04-24 14:47:10 +02:00
|
|
|
"umask", "alias", "unalias", "fg", "bg", "getopts":
|
2017-05-17 19:49:27 +02:00
|
|
|
r.runErr(pos, "unhandled builtin: %s", name)
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|
2017-05-17 19:49:27 +02:00
|
|
|
return 0
|
2017-04-24 14:47:10 +02:00
|
|
|
}
|