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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
package main
import (
"fmt"
"io/ioutil"
"netbsd.org/pkglint/regex"
"netbsd.org/pkglint/trace"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)
// Short names for commonly used functions.
func contains(s, substr string) bool {
return strings.Contains(s, substr)
}
func hasPrefix(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
}
func hasSuffix(s, suffix string) bool {
return strings.HasSuffix(s, suffix)
}
func matches(s string, re regex.Pattern) bool {
return regex.Matches(s, re)
}
func match1(s string, re regex.Pattern) (matched bool, m1 string) {
return regex.Match1(s, re)
}
func match2(s string, re regex.Pattern) (matched bool, m1, m2 string) {
return regex.Match2(s, re)
}
func match3(s string, re regex.Pattern) (matched bool, m1, m2, m3 string) {
return regex.Match3(s, re)
}
func match4(s string, re regex.Pattern) (matched bool, m1, m2, m3, m4 string) {
return regex.Match4(s, re)
}
func ifelseStr(cond bool, a, b string) string {
if cond {
return a
}
return b
}
func imax(a, b int) int {
if a > b {
return a
}
return b
}
func mustMatch(s string, re regex.Pattern) []string {
if m := regex.Match(s, re); m != nil {
return m
}
panic(fmt.Sprintf("mustMatch %q %q", s, re))
}
func isEmptyDir(fname string) bool {
dirents, err := ioutil.ReadDir(fname)
if err != nil || hasSuffix(fname, "/CVS") {
return true
}
for _, dirent := range dirents {
name := dirent.Name()
if isIgnoredFilename(name) {
continue
}
if dirent.IsDir() && isEmptyDir(fname+"/"+name) {
continue
}
return false
}
return true
}
func getSubdirs(fname string) []string {
dirents, err := ioutil.ReadDir(fname)
if err != nil {
NewLineWhole(fname).Fatalf("Cannot be read: %s", err)
}
var subdirs []string
for _, dirent := range dirents {
name := dirent.Name()
if dirent.IsDir() && !isIgnoredFilename(name) && !isEmptyDir(fname+"/"+name) {
subdirs = append(subdirs, name)
}
}
return subdirs
}
func isIgnoredFilename(fileName string) bool {
switch fileName {
case ".", "..", "CVS", ".svn", ".git", ".hg":
return true
}
return false
}
// Checks whether a file is already committed to the CVS repository.
func isCommitted(fname string) bool {
lines := loadCvsEntries(fname)
needle := "/" + path.Base(fname) + "/"
for _, line := range lines {
if hasPrefix(line.Text, needle) {
return true
}
}
return false
}
func isLocallyModified(fname string) bool {
lines := loadCvsEntries(fname)
needle := "/" + path.Base(fname) + "/"
for _, line := range lines {
if hasPrefix(line.Text, needle) {
cvsModTime, err := time.Parse(time.ANSIC, strings.Split(line.Text, "/")[3])
if err != nil {
return false
}
st, err := os.Stat(fname)
if err != nil {
return false
}
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724290(v=vs.85).aspx
// (System Services > Windows System Information > Time > About Time > File Times)
delta := cvsModTime.Unix() - st.ModTime().Unix()
if trace.Tracing {
trace.Stepf("cvs.time=%v fs.time=%v delta=%v", cvsModTime, st.ModTime(), delta)
}
return !(-2 <= delta && delta <= 2)
}
}
return false
}
func loadCvsEntries(fname string) []Line {
dir := path.Dir(fname)
if dir == G.CvsEntriesDir {
return G.CvsEntriesLines
}
lines, err := readLines(dir+"/CVS/Entries", false)
if err != nil {
return nil
}
G.CvsEntriesDir = dir
G.CvsEntriesLines = lines
return lines
}
// Returns the number of columns that a string occupies when printed with
// a tabulator size of 8.
func tabWidth(s string) int {
length := 0
for _, r := range s {
if r == '\t' {
length = length - length%8 + 8
} else {
length++
}
}
return length
}
func detab(s string) string {
detabbed := ""
for _, r := range s {
if r == '\t' {
detabbed += " "[:8-len(detabbed)%8]
} else {
detabbed += string(r)
}
}
return detabbed
}
func varnameBase(varname string) string {
dot := strings.IndexByte(varname, '.')
if dot != -1 {
return varname[:dot]
}
return varname
}
func varnameCanon(varname string) string {
dot := strings.IndexByte(varname, '.')
if dot != -1 {
return varname[:dot] + ".*"
}
return varname
}
func varnameParam(varname string) string {
dot := strings.IndexByte(varname, '.')
if dot != -1 {
return varname[dot+1:]
}
return ""
}
func defineVar(mkline MkLine, varname string) {
if G.Mk != nil {
G.Mk.DefineVar(mkline, varname)
}
if G.Pkg != nil {
G.Pkg.defineVar(mkline, varname)
}
}
func varIsDefined(varname string) bool {
varcanon := varnameCanon(varname)
if G.Mk != nil && (G.Mk.vardef[varname] != nil || G.Mk.vardef[varcanon] != nil) {
return true
}
if G.Pkg != nil && (G.Pkg.vardef[varname] != nil || G.Pkg.vardef[varcanon] != nil) {
return true
}
return false
}
func varIsUsed(varname string) bool {
varcanon := varnameCanon(varname)
if G.Mk != nil && (G.Mk.varuse[varname] != nil || G.Mk.varuse[varcanon] != nil) {
return true
}
if G.Pkg != nil && (G.Pkg.varuse[varname] != nil || G.Pkg.varuse[varcanon] != nil) {
return true
}
return false
}
func splitOnSpace(s string) []string {
return regex.Compile(`\s+`).Split(s, -1)
}
func fileExists(fname string) bool {
st, err := os.Stat(fname)
return err == nil && st.Mode().IsRegular()
}
func dirExists(fname string) bool {
st, err := os.Stat(fname)
return err == nil && st.Mode().IsDir()
}
// Useful in combination with regex.Find*Index
func negToZero(i int) int {
if i >= 0 {
return i
}
return 0
}
func toInt(s string, def int) int {
if n, err := strconv.Atoi(s); err == nil {
return n
}
return def
}
func dirglob(dirname string) []string {
fis, err := ioutil.ReadDir(dirname)
if err != nil {
return nil
}
var fnames []string
for _, fi := range fis {
if !(isIgnoredFilename(fi.Name())) {
fnames = append(fnames, dirname+"/"+fi.Name())
}
}
return fnames
}
// Emulates make(1)'s :S substitution operator.
func mkopSubst(s string, left bool, from string, right bool, to string, flags string) string {
if trace.Tracing {
defer trace.Call(s, left, from, right, to, flags)()
}
re := regex.Pattern(ifelseStr(left, "^", "") + regexp.QuoteMeta(from) + ifelseStr(right, "$", ""))
done := false
gflag := contains(flags, "g")
return regex.Compile(re).ReplaceAllStringFunc(s, func(match string) string {
if gflag || !done {
done = !gflag
return to
}
return match
})
}
func relpath(from, to string) string {
absFrom, err1 := filepath.Abs(from)
absTo, err2 := filepath.Abs(to)
rel, err3 := filepath.Rel(absFrom, absTo)
if err1 != nil || err2 != nil || err3 != nil {
trace.Stepf("relpath.panic", from, to, err1, err2, err3)
panic("relpath")
}
result := filepath.ToSlash(rel)
if trace.Tracing {
trace.Stepf("relpath from %q to %q = %q", from, to, result)
}
return result
}
func abspath(fname string) string {
abs, err := filepath.Abs(fname)
if err != nil {
NewLineWhole(fname).Fatalf("Cannot determine absolute path.")
}
return filepath.ToSlash(abs)
}
// Differs from path.Clean in that only "../../" is replaced, not "../".
// Also, the initial directory is always kept.
// This is to provide the package path as context in recursive invocations of pkglint.
func cleanpath(fname string) string {
tmp := fname
for len(tmp) > 2 && hasPrefix(tmp, "./") {
tmp = tmp[2:]
}
for contains(tmp, "/./") {
tmp = strings.Replace(tmp, "/./", "/", -1)
}
for contains(tmp, "//") {
tmp = strings.Replace(tmp, "//", "/", -1)
}
tmp = reReplaceRepeatedly(tmp, `/[^.][^/]*/[^.][^/]*/\.\./\.\./`, "/")
tmp = strings.TrimSuffix(tmp, "/")
return tmp
}
func containsVarRef(s string) bool {
return contains(s, "${")
}
func reReplaceRepeatedly(from string, re regex.Pattern, to string) string {
replaced := regex.Compile(re).ReplaceAllString(from, to)
if replaced != from {
return reReplaceRepeatedly(replaced, re, to)
}
return replaced
}
func hasAlnumPrefix(s string) bool {
if s == "" {
return false
}
b := s[0]
return '0' <= b && b <= '9' || 'A' <= b && b <= 'Z' || b == '_' || 'a' <= b && b <= 'z'
}
// Once remembers with which arguments its FirstTime method has been called
// and only returns true on each first call.
type Once struct {
seen map[string]bool
}
func (o *Once) FirstTime(what string) bool {
if o.seen == nil {
o.seen = make(map[string]bool)
}
if _, ok := o.seen[what]; ok {
return false
}
o.seen[what] = true
return true
}
|