2017-04-24 14:47:10 +02:00
|
|
|
// Copyright (c) 2017, Daniel Martí <mvdan@mvdan.cc>
|
|
|
|
// See LICENSE for licensing information
|
|
|
|
|
|
|
|
package interp
|
|
|
|
|
|
|
|
import (
|
2017-06-14 19:00:36 +02:00
|
|
|
"bytes"
|
2017-04-24 14:47:10 +02:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
|
2017-09-02 16:19:00 +02:00
|
|
|
"mvdan.cc/sh/syntax"
|
2017-04-24 14:47:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// non-empty string is true, empty string is false
|
|
|
|
func (r *Runner) bashTest(expr syntax.TestExpr) string {
|
|
|
|
switch x := expr.(type) {
|
|
|
|
case *syntax.Word:
|
|
|
|
return r.loneWord(x)
|
|
|
|
case *syntax.ParenTest:
|
|
|
|
return r.bashTest(x.X)
|
|
|
|
case *syntax.BinaryTest:
|
2017-06-14 19:00:36 +02:00
|
|
|
switch x.Op {
|
|
|
|
case syntax.TsMatch, syntax.TsNoMatch:
|
|
|
|
str := r.loneWord(x.X.(*syntax.Word))
|
|
|
|
var buf bytes.Buffer
|
|
|
|
yw := x.Y.(*syntax.Word)
|
|
|
|
for _, field := range r.wordFields(yw.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) == (x.Op == syntax.TsMatch) {
|
|
|
|
return "1"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2017-04-24 14:47:10 +02:00
|
|
|
if r.binTest(x.Op, r.bashTest(x.X), r.bashTest(x.Y)) {
|
|
|
|
return "1"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
case *syntax.UnaryTest:
|
|
|
|
if r.unTest(x.Op, r.bashTest(x.X)) {
|
|
|
|
return "1"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) binTest(op syntax.BinTestOperator, x, y string) bool {
|
|
|
|
switch op {
|
|
|
|
case syntax.TsReMatch:
|
|
|
|
re, err := regexp.Compile(y)
|
|
|
|
if err != nil {
|
|
|
|
r.exit = 2
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return re.MatchString(x)
|
|
|
|
case syntax.TsNewer:
|
2017-07-06 01:46:05 +02:00
|
|
|
i1, i2 := r.stat(x), r.stat(y)
|
2017-04-24 14:47:10 +02:00
|
|
|
if i1 == nil || i2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return i1.ModTime().After(i2.ModTime())
|
|
|
|
case syntax.TsOlder:
|
2017-07-06 01:46:05 +02:00
|
|
|
i1, i2 := r.stat(x), r.stat(y)
|
2017-04-24 14:47:10 +02:00
|
|
|
if i1 == nil || i2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return i1.ModTime().Before(i2.ModTime())
|
|
|
|
case syntax.TsDevIno:
|
2017-07-06 01:46:05 +02:00
|
|
|
i1, i2 := r.stat(x), r.stat(y)
|
2017-04-24 14:47:10 +02:00
|
|
|
return os.SameFile(i1, i2)
|
|
|
|
case syntax.TsEql:
|
|
|
|
return atoi(x) == atoi(y)
|
|
|
|
case syntax.TsNeq:
|
|
|
|
return atoi(x) != atoi(y)
|
|
|
|
case syntax.TsLeq:
|
|
|
|
return atoi(x) <= atoi(y)
|
|
|
|
case syntax.TsGeq:
|
|
|
|
return atoi(x) >= atoi(y)
|
|
|
|
case syntax.TsLss:
|
|
|
|
return atoi(x) < atoi(y)
|
|
|
|
case syntax.TsGtr:
|
|
|
|
return atoi(x) > atoi(y)
|
|
|
|
case syntax.AndTest:
|
|
|
|
return x != "" && y != ""
|
|
|
|
case syntax.OrTest:
|
|
|
|
return x != "" || y != ""
|
|
|
|
case syntax.TsBefore:
|
|
|
|
return x < y
|
|
|
|
default: // syntax.TsAfter
|
|
|
|
return x > y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 01:46:05 +02:00
|
|
|
func (r *Runner) stat(name string) os.FileInfo {
|
|
|
|
info, _ := os.Stat(r.relPath(name))
|
2017-04-24 14:47:10 +02:00
|
|
|
return info
|
|
|
|
}
|
|
|
|
|
2017-07-06 01:46:05 +02:00
|
|
|
func (r *Runner) statMode(name string, mode os.FileMode) bool {
|
|
|
|
info := r.stat(name)
|
2017-04-24 14:47:10 +02:00
|
|
|
return info != nil && info.Mode()&mode != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) unTest(op syntax.UnTestOperator, x string) bool {
|
|
|
|
switch op {
|
|
|
|
case syntax.TsExists:
|
2017-07-06 01:46:05 +02:00
|
|
|
return r.stat(x) != nil
|
2017-04-24 14:47:10 +02:00
|
|
|
case syntax.TsRegFile:
|
2017-07-06 01:46:05 +02:00
|
|
|
info := r.stat(x)
|
2017-04-24 14:47:10 +02:00
|
|
|
return info != nil && info.Mode().IsRegular()
|
|
|
|
case syntax.TsDirect:
|
2017-07-06 01:46:05 +02:00
|
|
|
return r.statMode(x, os.ModeDir)
|
2017-04-24 14:47:10 +02:00
|
|
|
//case syntax.TsCharSp:
|
|
|
|
//case syntax.TsBlckSp:
|
|
|
|
case syntax.TsNmPipe:
|
2017-07-06 01:46:05 +02:00
|
|
|
return r.statMode(x, os.ModeNamedPipe)
|
2017-04-24 14:47:10 +02:00
|
|
|
case syntax.TsSocket:
|
2017-07-06 01:46:05 +02:00
|
|
|
return r.statMode(x, os.ModeSocket)
|
2017-04-24 14:47:10 +02:00
|
|
|
case syntax.TsSmbLink:
|
2017-07-06 01:46:05 +02:00
|
|
|
info, _ := os.Lstat(r.relPath(x))
|
2017-04-24 14:47:10 +02:00
|
|
|
return info != nil && info.Mode()&os.ModeSymlink != 0
|
|
|
|
case syntax.TsSticky:
|
2017-07-06 01:46:05 +02:00
|
|
|
return r.statMode(x, os.ModeSticky)
|
2017-04-24 14:47:10 +02:00
|
|
|
case syntax.TsUIDSet:
|
2017-07-06 01:46:05 +02:00
|
|
|
return r.statMode(x, os.ModeSetuid)
|
2017-04-24 14:47:10 +02:00
|
|
|
case syntax.TsGIDSet:
|
2017-07-06 01:46:05 +02:00
|
|
|
return r.statMode(x, os.ModeSetgid)
|
2017-04-24 14:47:10 +02:00
|
|
|
//case syntax.TsGrpOwn:
|
|
|
|
//case syntax.TsUsrOwn:
|
|
|
|
//case syntax.TsModif:
|
|
|
|
case syntax.TsRead:
|
2017-10-15 21:41:15 +02:00
|
|
|
f, err := r.open(r.relPath(x), os.O_RDONLY, 0, false)
|
2017-04-24 14:47:10 +02:00
|
|
|
if err == nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
return err == nil
|
|
|
|
case syntax.TsWrite:
|
2017-10-15 21:41:15 +02:00
|
|
|
f, err := r.open(r.relPath(x), os.O_WRONLY, 0, false)
|
2017-04-24 14:47:10 +02:00
|
|
|
if err == nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
return err == nil
|
|
|
|
case syntax.TsExec:
|
2017-07-06 01:46:05 +02:00
|
|
|
_, err := exec.LookPath(r.relPath(x))
|
2017-04-24 14:47:10 +02:00
|
|
|
return err == nil
|
|
|
|
case syntax.TsNoEmpty:
|
2017-07-06 01:46:05 +02:00
|
|
|
info := r.stat(x)
|
2017-04-24 14:47:10 +02:00
|
|
|
return info != nil && info.Size() > 0
|
|
|
|
//case syntax.TsFdTerm:
|
|
|
|
case syntax.TsEmpStr:
|
|
|
|
return x == ""
|
|
|
|
case syntax.TsNempStr:
|
|
|
|
return x != ""
|
|
|
|
//case syntax.TsOptSet:
|
2017-05-27 16:17:49 +02:00
|
|
|
case syntax.TsVarSet:
|
|
|
|
_, e := r.lookupVar(x)
|
|
|
|
return e
|
2017-04-24 14:47:10 +02:00
|
|
|
//case syntax.TsRefVar:
|
|
|
|
case syntax.TsNot:
|
|
|
|
return x == ""
|
|
|
|
default:
|
2017-07-06 01:46:05 +02:00
|
|
|
r.runErr(syntax.Pos{}, "unhandled unary test op: %v", op)
|
2017-04-24 14:47:10 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|