| 1 |
// Copyright (c) 2017, Daniel Martà <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
package interp |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
"regexp" |
| 11 |
|
| 12 |
"golang.org/x/term" |
| 13 |
|
| 14 |
"mvdan.cc/sh/v3/expand" |
| 15 |
"mvdan.cc/sh/v3/syntax" |
| 16 |
) |
| 17 |
|
| 18 |
// non-empty string is true, empty string is false |
| 19 |
func (r *Runner) bashTest(ctx context.Context, expr syntax.TestExpr, classic bool) string { |
| 20 |
switch x := expr.(type) { |
| 21 |
case *syntax.Word: |
| 22 |
if classic { |
| 23 |
// In the classic "test" mode, we already expanded and |
| 24 |
// split the list of words, so don't redo that work. |
| 25 |
return r.document(x) |
| 26 |
} |
| 27 |
return r.literal(x) |
| 28 |
case *syntax.ParenTest: |
| 29 |
return r.bashTest(ctx, x.X, classic) |
| 30 |
case *syntax.BinaryTest: |
| 31 |
switch x.Op { |
| 32 |
case syntax.TsMatchShort, syntax.TsMatch, syntax.TsNoMatch: |
| 33 |
str := r.literal(x.X.(*syntax.Word)) |
| 34 |
yw := x.Y.(*syntax.Word) |
| 35 |
if classic { // test, [ |
| 36 |
lit := r.literal(yw) |
| 37 |
if (str == lit) == (x.Op != syntax.TsNoMatch) { |
| 38 |
return "1" |
| 39 |
} |
| 40 |
} else { // [[ |
| 41 |
pattern := r.pattern(yw) |
| 42 |
if r.match(pattern, str) == (x.Op != syntax.TsNoMatch) { |
| 43 |
return "1" |
| 44 |
} |
| 45 |
} |
| 46 |
return "" |
| 47 |
} |
| 48 |
if r.binTest(ctx, x.Op, r.bashTest(ctx, x.X, classic), r.bashTest(ctx, x.Y, classic)) { |
| 49 |
return "1" |
| 50 |
} |
| 51 |
return "" |
| 52 |
case *syntax.UnaryTest: |
| 53 |
if r.unTest(ctx, x.Op, r.bashTest(ctx, x.X, classic)) { |
| 54 |
return "1" |
| 55 |
} |
| 56 |
return "" |
| 57 |
} |
| 58 |
return "" |
| 59 |
} |
| 60 |
|
| 61 |
func (r *Runner) binTest(ctx context.Context, op syntax.BinTestOperator, x, y string) bool { |
| 62 |
switch op { |
| 63 |
case syntax.TsReMatch: |
| 64 |
re, err := regexp.Compile(y) |
| 65 |
if err != nil { |
| 66 |
r.exit.code = 2 |
| 67 |
return false |
| 68 |
} |
| 69 |
m := re.FindStringSubmatch(x) |
| 70 |
if m == nil { |
| 71 |
return false |
| 72 |
} |
| 73 |
vr := expand.Variable{ |
| 74 |
Set: true, |
| 75 |
Kind: expand.Indexed, |
| 76 |
List: m, |
| 77 |
} |
| 78 |
r.setVar("BASH_REMATCH", vr) |
| 79 |
return true |
| 80 |
case syntax.TsNewer, syntax.TsOlder: |
| 81 |
info1, err1 := r.stat(ctx, x) |
| 82 |
info2, err2 := r.stat(ctx, y) |
| 83 |
// -ot is the mirror of -nt, so swap the operands and share the logic. |
| 84 |
if op == syntax.TsOlder { |
| 85 |
info1, info2, err1, err2 = info2, info1, err2, err1 |
| 86 |
} |
| 87 |
// True if the first operand exists and the second does not, |
| 88 |
// or if both exist and the first is newer. |
| 89 |
if err1 != nil { |
| 90 |
return false |
| 91 |
} |
| 92 |
if err2 != nil { |
| 93 |
return true |
| 94 |
} |
| 95 |
return info1.ModTime().After(info2.ModTime()) |
| 96 |
case syntax.TsDevIno: |
| 97 |
info1, err1 := r.stat(ctx, x) |
| 98 |
info2, err2 := r.stat(ctx, y) |
| 99 |
if err1 != nil || err2 != nil { |
| 100 |
return false |
| 101 |
} |
| 102 |
return os.SameFile(info1, info2) |
| 103 |
case syntax.TsEql: |
| 104 |
return atoi(x) == atoi(y) |
| 105 |
case syntax.TsNeq: |
| 106 |
return atoi(x) != atoi(y) |
| 107 |
case syntax.TsLeq: |
| 108 |
return atoi(x) <= atoi(y) |
| 109 |
case syntax.TsGeq: |
| 110 |
return atoi(x) >= atoi(y) |
| 111 |
case syntax.TsLss: |
| 112 |
return atoi(x) < atoi(y) |
| 113 |
case syntax.TsGtr: |
| 114 |
return atoi(x) > atoi(y) |
| 115 |
case syntax.AndTest: |
| 116 |
return x != "" && y != "" |
| 117 |
case syntax.OrTest: |
| 118 |
return x != "" || y != "" |
| 119 |
case syntax.TsBefore: |
| 120 |
return x < y |
| 121 |
case syntax.TsAfter: |
| 122 |
return x > y |
| 123 |
default: |
| 124 |
// Should only happen if we forgot a case above. |
| 125 |
panic(fmt.Sprintf("unexpected binary test operator: %q", op)) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func (r *Runner) statMode(ctx context.Context, name string, mode os.FileMode) bool { |
| 130 |
info, err := r.stat(ctx, name) |
| 131 |
return err == nil && info.Mode()&mode != 0 |
| 132 |
} |
| 133 |
|
| 134 |
func (r *Runner) unTest(ctx context.Context, op syntax.UnTestOperator, x string) bool { |
| 135 |
switch op { |
| 136 |
case syntax.TsExists: |
| 137 |
_, err := r.stat(ctx, x) |
| 138 |
return err == nil |
| 139 |
case syntax.TsRegFile: |
| 140 |
info, err := r.stat(ctx, x) |
| 141 |
return err == nil && info.Mode().IsRegular() |
| 142 |
case syntax.TsDirect: |
| 143 |
return r.statMode(ctx, x, os.ModeDir) |
| 144 |
case syntax.TsCharSp: |
| 145 |
return r.statMode(ctx, x, os.ModeCharDevice) |
| 146 |
case syntax.TsBlckSp: |
| 147 |
info, err := r.stat(ctx, x) |
| 148 |
return err == nil && info.Mode()&os.ModeDevice != 0 && |
| 149 |
info.Mode()&os.ModeCharDevice == 0 |
| 150 |
case syntax.TsNmPipe: |
| 151 |
return r.statMode(ctx, x, os.ModeNamedPipe) |
| 152 |
case syntax.TsSocket: |
| 153 |
return r.statMode(ctx, x, os.ModeSocket) |
| 154 |
case syntax.TsSmbLink: |
| 155 |
info, err := r.lstat(ctx, x) |
| 156 |
return err == nil && info.Mode()&os.ModeSymlink != 0 |
| 157 |
case syntax.TsSticky: |
| 158 |
return r.statMode(ctx, x, os.ModeSticky) |
| 159 |
case syntax.TsUIDSet: |
| 160 |
return r.statMode(ctx, x, os.ModeSetuid) |
| 161 |
case syntax.TsGIDSet: |
| 162 |
return r.statMode(ctx, x, os.ModeSetgid) |
| 163 |
case syntax.TsModif: |
| 164 |
info, err := r.stat(ctx, x) |
| 165 |
if err != nil { |
| 166 |
return false |
| 167 |
} |
| 168 |
return info.ModTime().After(getAtime(info)) |
| 169 |
case syntax.TsRead: |
| 170 |
return r.access(ctx, x, AccessRead) == nil |
| 171 |
case syntax.TsWrite: |
| 172 |
return r.access(ctx, x, AccessWrite) == nil |
| 173 |
case syntax.TsExec: |
| 174 |
return r.access(ctx, x, AccessExec) == nil |
| 175 |
case syntax.TsNoEmpty: |
| 176 |
info, err := r.stat(ctx, x) |
| 177 |
return err == nil && info.Size() > 0 |
| 178 |
case syntax.TsFdTerm: |
| 179 |
fd := atoi(x) |
| 180 |
var f any |
| 181 |
switch fd { |
| 182 |
case 0: |
| 183 |
f = r.stdin |
| 184 |
case 1: |
| 185 |
f = r.stdout |
| 186 |
case 2: |
| 187 |
f = r.stderr |
| 188 |
} |
| 189 |
if f, ok := f.(interface{ Fd() uintptr }); ok { |
| 190 |
// Support [os.File.Fd] methods such as the one on [*os.File]. |
| 191 |
return term.IsTerminal(int(f.Fd())) |
| 192 |
} |
| 193 |
// TODO: allow term.IsTerminal here too if running in the |
| 194 |
// "single process" mode. |
| 195 |
return false |
| 196 |
case syntax.TsEmpStr: |
| 197 |
return x == "" |
| 198 |
case syntax.TsNempStr: |
| 199 |
return x != "" |
| 200 |
case syntax.TsOptSet: |
| 201 |
if opt := r.posixOptByName(x); opt != nil { |
| 202 |
return *opt |
| 203 |
} |
| 204 |
return false |
| 205 |
case syntax.TsVarSet: |
| 206 |
return r.lookupVar(x).IsSet() |
| 207 |
case syntax.TsRefVar: |
| 208 |
return r.lookupVar(x).Kind == expand.NameRef |
| 209 |
case syntax.TsNot: |
| 210 |
return x == "" |
| 211 |
case syntax.TsUsrOwn, syntax.TsGrpOwn: |
| 212 |
return r.unTestOwnOrGrp(ctx, op, x) |
| 213 |
default: |
| 214 |
// Should only happen if we forgot a case above. |
| 215 |
panic(fmt.Sprintf("unexpected unary test op: %v", op)) |
| 216 |
} |
| 217 |
} |