@@ -95,15 +95,20 @@ func expandfile(t *Text, q0 int, q1 int, e *Expand) (success bool) {
95
95
if found == nil {
96
96
found = filenameRE .FindForward (rb , 0 , n , 1 )
97
97
}
98
+ matchedone := false
98
99
for i , pair := range found {
99
100
// qq0 is zero of the range returned.
100
101
// The match is thus at qq0+pair[0:1], and (q0-qq0) must fall in that range
101
102
if q0 - qq0 >= pair [0 ] && q0 - qq0 <= pair [1 ] {
102
103
q0 , q1 = pair [0 ]+ qq0 , pair [1 ]+ qq0
103
104
found = found [i : i + 1 ]
105
+ matchedone = true
104
106
break
105
107
}
106
108
}
109
+ if ! matchedone {
110
+ return false
111
+ }
107
112
} else {
108
113
n = q1 - q0
109
114
if n == 0 {
@@ -147,18 +152,42 @@ func expandfile(t *Text, q0 int, q1 int, e *Expand) (success bool) {
147
152
func (q int ) rune { return t .ReadC (q ) }, false )
148
153
return true
149
154
}
150
- s := filename
151
- if amin == q0 {
152
- return isFile (filename )
153
- }
154
- dname := t .DirName (s )
155
- // if it's already a window name, it's a file
156
- if lookfile (dname ) != nil {
157
- return isFile (dname )
158
- }
159
- // if it's the name of a file, it's a file
160
- if ismtpt (dname ) || ! access (dname ) {
161
- return false
155
+
156
+ // we iterate over the candidate filename, removing trailing words
157
+ for {
158
+ dname := t .DirName (filename )
159
+ switch {
160
+ case amin == q0 :
161
+ if isFile (filename ) {
162
+ return true
163
+ }
164
+ // if it's already a window name, it's a file
165
+ case lookfile (dname ) != nil :
166
+ if isFile (dname ) {
167
+ return true
168
+ }
169
+ // if it's the name of a file, it's a file
170
+ case ismtpt (dname ) || ! access (dname ):
171
+ return false
172
+ }
173
+
174
+ if isFile (dname ) {
175
+ return true
176
+ }
177
+ var q1 int
178
+ for q1 = e .q1 ; q1 >= e .q0 ; q1 -- {
179
+ if rb [q1 - 1 ] == '\\' || rb [q1 - 1 ] == '/' {
180
+ // don't back up past a path separator
181
+ return false
182
+ }
183
+ if rb [q1 - 1 ] == ' ' || rb [q1 - 1 ] == '\t' {
184
+ filename = string (rb [e .q0 :e .q1 ])
185
+ e .q1 = q1
186
+ break
187
+ }
188
+ }
189
+ if q1 < e .q0 {
190
+ return false
191
+ }
162
192
}
163
- return isFile (dname )
164
193
}
0 commit comments