-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.go
More file actions
163 lines (150 loc) · 3.57 KB
/
utils.go
File metadata and controls
163 lines (150 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"errors"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
)
func UnescapeLiteral(s string, inString bool) string {
if inString {
return stringEscapeSeqs.ReplaceAllStringFunc(s, replaceStringEscapeSeq)
}
return literalEscapeSeqs.ReplaceAllStringFunc(s, replaceLiteralEscapeSeq)
}
// This function replaces escape sequences with the values they escape.
// Lifted from Golua, probably needs adapting better to a shell context.
func replaceStringEscapeSeq(e string) string {
switch e[1] {
case 'a':
return "\a"
case 'b':
return "\b"
case 't':
return "\t"
case 'n':
return "\n"
case 'v':
return "\v"
case 'f':
return "\f"
case 'r':
return "\r"
case 'x', 'X':
b, err := strconv.ParseInt(e[2:], 16, 64)
if err != nil {
panic(err)
}
return string(rune(b))
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
b, err := strconv.ParseInt(string(e[1:]), 10, 64)
if err != nil {
panic(err)
}
if b >= 256 {
panic(errors.New("decimal escape sequence out of range"))
}
return string(rune(b))
case 'u', 'U':
i, err := strconv.ParseInt(string(e[3:len(e)-1]), 16, 32)
if err != nil {
panic(err)
}
if i >= 0x110000 {
panic(errors.New("unicode escape sequence out of range"))
}
return string(rune(i))
default:
return e
}
}
// This regex matches all the escape sequences that can be found in a Lua string.
var stringEscapeSeqs = regexp.MustCompile(`(?s)\\\d{1,3}|\\[xX][0-9a-fA-F]{2}|\\[abtnvfr\\]|\\[uU]{[0-9a-fA-F]+}|\\.`)
func replaceLiteralEscapeSeq(e string) string {
switch e[1] {
case '\n':
return ""
default:
return e[1:]
}
}
var literalEscapeSeqs = regexp.MustCompile(`\\.`)
//
// The following is lifted and slightly adapted from the go package exec
// (lp_unix.go). I can't reuse it as is because of the use of os.Getenv("PATH")
// in the original code.
//
func findExecutable(file string) error {
d, err := os.Stat(file)
if err != nil {
return err
}
if m := d.Mode(); !m.IsDir() && m&0111 != 0 {
return nil
}
return fs.ErrPermission
}
// LookPath searches for an executable named file in the
// directories named by the path.
// If file contains a slash, it is tried directly and path is not consulted.
// The result may be an absolute path or a path relative to the current directory.
func LookPath(path, wd, file string) (string, error) {
if strings.HasPrefix(file, "/") {
err := findExecutable(file)
if err == nil {
return file, nil
}
return "", &exec.Error{Name: file, Err: err}
}
if strings.Contains(file, "/") {
path := filepath.Join(wd, file)
err := findExecutable(path)
if err == nil {
return path, nil
}
return "", &exec.Error{Name: file, Err: err}
}
for _, dir := range filepath.SplitList(path) {
if dir == "" {
// Unix shell semantics: path element "" means "."
dir = wd
}
path := filepath.Join(dir, file)
if err := findExecutable(path); err == nil {
return path, nil
}
}
return "", &exec.Error{Name: file, Err: exec.ErrNotFound}
}
//
// End of lifting
//
var ErrNotADirectory = errors.New("file is not a directory")
func findDirectory(file string) error {
d, err := os.Stat(file)
if err != nil {
return err
}
if m := d.Mode(); m.IsDir() {
return nil
}
return ErrNotADirectory
}
func LookDir(wd, file string) (string, error) {
if strings.HasPrefix(file, "/") {
err := findDirectory(file)
if err == nil {
return file, nil
}
return "", &exec.Error{Name: file, Err: err}
}
path := filepath.Join(wd, file)
err := findDirectory(path)
if err == nil {
return path, nil
}
return "", &exec.Error{Name: file, Err: err}
}