Skip to content

Commit

Permalink
Fix expandfile windows (#492)
Browse files Browse the repository at this point in the history
* Fix expand file for windows

q0 within the found file selection test was broken.
Also, try peeling off trailing words that aren't parts
of a found filename.

* Fix possible infinite loop on filename trimming

---------

Co-authored-by: Paul Lalonde <[email protected]>
  • Loading branch information
paul-lalonde and plalonde-nvidia authored May 10, 2023
1 parent b54e04d commit e347c3d
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions expandfile_win.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,20 @@ func expandfile(t *Text, q0 int, q1 int, e *Expand) (success bool) {
if found == nil {
found = filenameRE.FindForward(rb, 0, n, 1)
}
matchedone := false
for i, pair := range found {
// qq0 is zero of the range returned.
// The match is thus at qq0+pair[0:1], and (q0-qq0) must fall in that range
if q0-qq0 >= pair[0] && q0-qq0 <= pair[1] {
q0, q1 = pair[0]+qq0, pair[1]+qq0
found = found[i : i+1]
matchedone = true
break
}
}
if !matchedone {
return false
}
} else {
n = q1 - q0
if n == 0 {
Expand Down Expand Up @@ -147,18 +152,42 @@ func expandfile(t *Text, q0 int, q1 int, e *Expand) (success bool) {
func(q int) rune { return t.ReadC(q) }, false)
return true
}
s := filename
if amin == q0 {
return isFile(filename)
}
dname := t.DirName(s)
// if it's already a window name, it's a file
if lookfile(dname) != nil {
return isFile(dname)
}
// if it's the name of a file, it's a file
if ismtpt(dname) || !access(dname) {
return false

// we iterate over the candidate filename, removing trailing words
for {
dname := t.DirName(filename)
switch {
case amin == q0:
if isFile(filename) {
return true
}
// if it's already a window name, it's a file
case lookfile(dname) != nil:
if isFile(dname) {
return true
}
// if it's the name of a file, it's a file
case ismtpt(dname) || !access(dname):
return false
}

if isFile(dname) {
return true
}
var q1 int
for q1 = e.q1; q1 >= e.q0; q1-- {
if rb[q1-1] == '\\' || rb[q1-1] == '/' {
// don't back up past a path separator
return false
}
if rb[q1-1] == ' ' || rb[q1-1] == '\t' {
filename = string(rb[e.q0:e.q1])
e.q1 = q1
break
}
}
if q1 < e.q0 {
return false
}
}
return isFile(dname)
}

0 comments on commit e347c3d

Please sign in to comment.