Mercurial > vim
comparison src/filepath.c @ 17978:8f4cc259ed7a v8.1.1985
patch 8.1.1985: code for dealing with paths is spread out
Commit: https://github.com/vim/vim/commit/26262f87770d3a1a68b09a70152d75c2e2ae186f
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Sep 4 20:59:15 2019 +0200
patch 8.1.1985: code for dealing with paths is spread out
Problem: Code for dealing with paths is spread out.
Solution: Move path related functions from misc1.c to filepath.c.
Remove NO_EXPANDPATH.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Wed, 04 Sep 2019 21:00:04 +0200 |
parents | 46f95606b9ec |
children | 5c8906f653f5 |
comparison
equal
deleted
inserted
replaced
17977:7fa0c4b6bfa5 | 17978:8f4cc259ed7a |
---|---|
34 | 34 |
35 len = *fnamelen; | 35 len = *fnamelen; |
36 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len); | 36 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len); |
37 if (l > len - 1) | 37 if (l > len - 1) |
38 { | 38 { |
39 /* If that doesn't work (not enough space), then save the string | 39 // If that doesn't work (not enough space), then save the string |
40 * and try again with a new buffer big enough. */ | 40 // and try again with a new buffer big enough. |
41 newbuf = vim_strnsave(*fnamep, l); | 41 newbuf = vim_strnsave(*fnamep, l); |
42 if (newbuf == NULL) | 42 if (newbuf == NULL) |
43 return FAIL; | 43 return FAIL; |
44 | 44 |
45 vim_free(*bufp); | 45 vim_free(*bufp); |
46 *fnamep = *bufp = newbuf; | 46 *fnamep = *bufp = newbuf; |
47 | 47 |
48 /* Really should always succeed, as the buffer is big enough. */ | 48 // Really should always succeed, as the buffer is big enough. |
49 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1); | 49 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1); |
50 } | 50 } |
51 | 51 |
52 *fnamelen = l; | 52 *fnamelen = l; |
53 return OK; | 53 return OK; |
79 char_u ch; | 79 char_u ch; |
80 int old_len, len; | 80 int old_len, len; |
81 int new_len, sfx_len; | 81 int new_len, sfx_len; |
82 int retval = OK; | 82 int retval = OK; |
83 | 83 |
84 /* Make a copy */ | 84 // Make a copy |
85 old_len = *fnamelen; | 85 old_len = *fnamelen; |
86 save_fname = vim_strnsave(*fname, old_len); | 86 save_fname = vim_strnsave(*fname, old_len); |
87 pbuf_unused = NULL; | 87 pbuf_unused = NULL; |
88 short_fname = NULL; | 88 short_fname = NULL; |
89 | 89 |
90 endp = save_fname + old_len - 1; /* Find the end of the copy */ | 90 endp = save_fname + old_len - 1; // Find the end of the copy |
91 save_endp = endp; | 91 save_endp = endp; |
92 | 92 |
93 /* | 93 /* |
94 * Try shortening the supplied path till it succeeds by removing one | 94 * Try shortening the supplied path till it succeeds by removing one |
95 * directory at a time from the tail of the path. | 95 * directory at a time from the tail of the path. |
96 */ | 96 */ |
97 len = 0; | 97 len = 0; |
98 for (;;) | 98 for (;;) |
99 { | 99 { |
100 /* go back one path-separator */ | 100 // go back one path-separator |
101 while (endp > save_fname && !after_pathsep(save_fname, endp + 1)) | 101 while (endp > save_fname && !after_pathsep(save_fname, endp + 1)) |
102 --endp; | 102 --endp; |
103 if (endp <= save_fname) | 103 if (endp <= save_fname) |
104 break; /* processed the complete path */ | 104 break; // processed the complete path |
105 | 105 |
106 /* | 106 /* |
107 * Replace the path separator with a NUL and try to shorten the | 107 * Replace the path separator with a NUL and try to shorten the |
108 * resulting path. | 108 * resulting path. |
109 */ | 109 */ |
114 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL) | 114 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL) |
115 { | 115 { |
116 retval = FAIL; | 116 retval = FAIL; |
117 goto theend; | 117 goto theend; |
118 } | 118 } |
119 *endp = ch; /* preserve the string */ | 119 *endp = ch; // preserve the string |
120 | 120 |
121 if (len > 0) | 121 if (len > 0) |
122 break; /* successfully shortened the path */ | 122 break; // successfully shortened the path |
123 | 123 |
124 /* failed to shorten the path. Skip the path separator */ | 124 // failed to shorten the path. Skip the path separator |
125 --endp; | 125 --endp; |
126 } | 126 } |
127 | 127 |
128 if (len > 0) | 128 if (len > 0) |
129 { | 129 { |
138 | 138 |
139 *fnamelen = new_len; | 139 *fnamelen = new_len; |
140 vim_free(*bufp); | 140 vim_free(*bufp); |
141 if (new_len > old_len) | 141 if (new_len > old_len) |
142 { | 142 { |
143 /* There is not enough space in the currently allocated string, | 143 // There is not enough space in the currently allocated string, |
144 * copy it to a buffer big enough. */ | 144 // copy it to a buffer big enough. |
145 *fname = *bufp = vim_strnsave(short_fname, new_len); | 145 *fname = *bufp = vim_strnsave(short_fname, new_len); |
146 if (*fname == NULL) | 146 if (*fname == NULL) |
147 { | 147 { |
148 retval = FAIL; | 148 retval = FAIL; |
149 goto theend; | 149 goto theend; |
150 } | 150 } |
151 } | 151 } |
152 else | 152 else |
153 { | 153 { |
154 /* Transfer short_fname to the main buffer (it's big enough), | 154 // Transfer short_fname to the main buffer (it's big enough), |
155 * unless get_short_pathname() did its work in-place. */ | 155 // unless get_short_pathname() did its work in-place. |
156 *fname = *bufp = save_fname; | 156 *fname = *bufp = save_fname; |
157 if (short_fname != save_fname) | 157 if (short_fname != save_fname) |
158 vim_strncpy(save_fname, short_fname, len); | 158 vim_strncpy(save_fname, short_fname, len); |
159 save_fname = NULL; | 159 save_fname = NULL; |
160 } | 160 } |
161 | 161 |
162 /* concat the not-shortened part of the path */ | 162 // concat the not-shortened part of the path |
163 vim_strncpy(*fname + len, endp, sfx_len); | 163 vim_strncpy(*fname + len, endp, sfx_len); |
164 (*fname)[new_len] = NUL; | 164 (*fname)[new_len] = NUL; |
165 } | 165 } |
166 | 166 |
167 theend: | 167 theend: |
184 int sepcount, len, tflen; | 184 int sepcount, len, tflen; |
185 char_u *p; | 185 char_u *p; |
186 char_u *pbuf, *tfname; | 186 char_u *pbuf, *tfname; |
187 int hasTilde; | 187 int hasTilde; |
188 | 188 |
189 /* Count up the path separators from the RHS.. so we know which part | 189 // Count up the path separators from the RHS.. so we know which part |
190 * of the path to return. */ | 190 // of the path to return. |
191 sepcount = 0; | 191 sepcount = 0; |
192 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p)) | 192 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p)) |
193 if (vim_ispathsep(*p)) | 193 if (vim_ispathsep(*p)) |
194 ++sepcount; | 194 ++sepcount; |
195 | 195 |
196 /* Need full path first (use expand_env() to remove a "~/") */ | 196 // Need full path first (use expand_env() to remove a "~/") |
197 hasTilde = (**fnamep == '~'); | 197 hasTilde = (**fnamep == '~'); |
198 if (hasTilde) | 198 if (hasTilde) |
199 pbuf = tfname = expand_env_save(*fnamep); | 199 pbuf = tfname = expand_env_save(*fnamep); |
200 else | 200 else |
201 pbuf = tfname = FullName_save(*fnamep, FALSE); | 201 pbuf = tfname = FullName_save(*fnamep, FALSE); |
205 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL) | 205 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL) |
206 return FAIL; | 206 return FAIL; |
207 | 207 |
208 if (len == 0) | 208 if (len == 0) |
209 { | 209 { |
210 /* Don't have a valid filename, so shorten the rest of the | 210 // Don't have a valid filename, so shorten the rest of the |
211 * path if we can. This CAN give us invalid 8.3 filenames, but | 211 // path if we can. This CAN give us invalid 8.3 filenames, but |
212 * there's not a lot of point in guessing what it might be. | 212 // there's not a lot of point in guessing what it might be. |
213 */ | |
214 len = tflen; | 213 len = tflen; |
215 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL) | 214 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL) |
216 return FAIL; | 215 return FAIL; |
217 } | 216 } |
218 | 217 |
219 /* Count the paths backward to find the beginning of the desired string. */ | 218 // Count the paths backward to find the beginning of the desired string. |
220 for (p = tfname + len - 1; p >= tfname; --p) | 219 for (p = tfname + len - 1; p >= tfname; --p) |
221 { | 220 { |
222 if (has_mbyte) | 221 if (has_mbyte) |
223 p -= mb_head_off(tfname, p); | 222 p -= mb_head_off(tfname, p); |
224 if (vim_ispathsep(*p)) | 223 if (vim_ispathsep(*p)) |
238 return FAIL; | 237 return FAIL; |
239 } | 238 } |
240 else | 239 else |
241 ++p; | 240 ++p; |
242 | 241 |
243 /* Copy in the string - p indexes into tfname - allocated at pbuf */ | 242 // Copy in the string - p indexes into tfname - allocated at pbuf |
244 vim_free(*bufp); | 243 vim_free(*bufp); |
245 *fnamelen = (int)STRLEN(p); | 244 *fnamelen = (int)STRLEN(p); |
246 *bufp = pbuf; | 245 *bufp = pbuf; |
247 *fnamep = p; | 246 *fnamep = p; |
248 | 247 |
276 char_u *fname_start = *fnamep; | 275 char_u *fname_start = *fnamep; |
277 int has_shortname = 0; | 276 int has_shortname = 0; |
278 #endif | 277 #endif |
279 | 278 |
280 repeat: | 279 repeat: |
281 /* ":p" - full path/file_name */ | 280 // ":p" - full path/file_name |
282 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p') | 281 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p') |
283 { | 282 { |
284 has_fullname = 1; | 283 has_fullname = 1; |
285 | 284 |
286 valid |= VALID_PATH; | 285 valid |= VALID_PATH; |
287 *usedlen += 2; | 286 *usedlen += 2; |
288 | 287 |
289 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */ | 288 // Expand "~/path" for all systems and "~user/path" for Unix and VMS |
290 if ((*fnamep)[0] == '~' | 289 if ((*fnamep)[0] == '~' |
291 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME)) | 290 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME)) |
292 && ((*fnamep)[1] == '/' | 291 && ((*fnamep)[1] == '/' |
293 # ifdef BACKSLASH_IN_FILENAME | 292 # ifdef BACKSLASH_IN_FILENAME |
294 || (*fnamep)[1] == '\\' | 293 || (*fnamep)[1] == '\\' |
297 #endif | 296 #endif |
298 && !(tilde_file && (*fnamep)[1] == NUL) | 297 && !(tilde_file && (*fnamep)[1] == NUL) |
299 ) | 298 ) |
300 { | 299 { |
301 *fnamep = expand_env_save(*fnamep); | 300 *fnamep = expand_env_save(*fnamep); |
302 vim_free(*bufp); /* free any allocated file name */ | 301 vim_free(*bufp); // free any allocated file name |
303 *bufp = *fnamep; | 302 *bufp = *fnamep; |
304 if (*fnamep == NULL) | 303 if (*fnamep == NULL) |
305 return -1; | 304 return -1; |
306 } | 305 } |
307 | 306 |
308 /* When "/." or "/.." is used: force expansion to get rid of it. */ | 307 // When "/." or "/.." is used: force expansion to get rid of it. |
309 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p)) | 308 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p)) |
310 { | 309 { |
311 if (vim_ispathsep(*p) | 310 if (vim_ispathsep(*p) |
312 && p[1] == '.' | 311 && p[1] == '.' |
313 && (p[2] == NUL | 312 && (p[2] == NUL |
315 || (p[2] == '.' | 314 || (p[2] == '.' |
316 && (p[3] == NUL || vim_ispathsep(p[3]))))) | 315 && (p[3] == NUL || vim_ispathsep(p[3]))))) |
317 break; | 316 break; |
318 } | 317 } |
319 | 318 |
320 /* FullName_save() is slow, don't use it when not needed. */ | 319 // FullName_save() is slow, don't use it when not needed. |
321 if (*p != NUL || !vim_isAbsName(*fnamep)) | 320 if (*p != NUL || !vim_isAbsName(*fnamep)) |
322 { | 321 { |
323 *fnamep = FullName_save(*fnamep, *p != NUL); | 322 *fnamep = FullName_save(*fnamep, *p != NUL); |
324 vim_free(*bufp); /* free any allocated file name */ | 323 vim_free(*bufp); // free any allocated file name |
325 *bufp = *fnamep; | 324 *bufp = *fnamep; |
326 if (*fnamep == NULL) | 325 if (*fnamep == NULL) |
327 return -1; | 326 return -1; |
328 } | 327 } |
329 | 328 |
352 vim_free(wfname); | 351 vim_free(wfname); |
353 } | 352 } |
354 } | 353 } |
355 # endif | 354 # endif |
356 #endif | 355 #endif |
357 /* Append a path separator to a directory. */ | 356 // Append a path separator to a directory. |
358 if (mch_isdir(*fnamep)) | 357 if (mch_isdir(*fnamep)) |
359 { | 358 { |
360 /* Make room for one or two extra characters. */ | 359 // Make room for one or two extra characters. |
361 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2); | 360 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2); |
362 vim_free(*bufp); /* free any allocated file name */ | 361 vim_free(*bufp); // free any allocated file name |
363 *bufp = *fnamep; | 362 *bufp = *fnamep; |
364 if (*fnamep == NULL) | 363 if (*fnamep == NULL) |
365 return -1; | 364 return -1; |
366 add_pathsep(*fnamep); | 365 add_pathsep(*fnamep); |
367 } | 366 } |
368 } | 367 } |
369 | 368 |
370 /* ":." - path relative to the current directory */ | 369 // ":." - path relative to the current directory |
371 /* ":~" - path relative to the home directory */ | 370 // ":~" - path relative to the home directory |
372 /* ":8" - shortname path - postponed till after */ | 371 // ":8" - shortname path - postponed till after |
373 while (src[*usedlen] == ':' | 372 while (src[*usedlen] == ':' |
374 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8')) | 373 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8')) |
375 { | 374 { |
376 *usedlen += 2; | 375 *usedlen += 2; |
377 if (c == '8') | 376 if (c == '8') |
378 { | 377 { |
379 #ifdef MSWIN | 378 #ifdef MSWIN |
380 has_shortname = 1; /* Postpone this. */ | 379 has_shortname = 1; // Postpone this. |
381 #endif | 380 #endif |
382 continue; | 381 continue; |
383 } | 382 } |
384 pbuf = NULL; | 383 pbuf = NULL; |
385 /* Need full path first (use expand_env() to remove a "~/") */ | 384 // Need full path first (use expand_env() to remove a "~/") |
386 if (!has_fullname) | 385 if (!has_fullname) |
387 { | 386 { |
388 if (c == '.' && **fnamep == '~') | 387 if (c == '.' && **fnamep == '~') |
389 p = pbuf = expand_env_save(*fnamep); | 388 p = pbuf = expand_env_save(*fnamep); |
390 else | 389 else |
404 if (s != NULL) | 403 if (s != NULL) |
405 { | 404 { |
406 *fnamep = s; | 405 *fnamep = s; |
407 if (pbuf != NULL) | 406 if (pbuf != NULL) |
408 { | 407 { |
409 vim_free(*bufp); /* free any allocated file name */ | 408 vim_free(*bufp); // free any allocated file name |
410 *bufp = pbuf; | 409 *bufp = pbuf; |
411 pbuf = NULL; | 410 pbuf = NULL; |
412 } | 411 } |
413 } | 412 } |
414 } | 413 } |
415 else | 414 else |
416 { | 415 { |
417 home_replace(NULL, p, dirname, MAXPATHL, TRUE); | 416 home_replace(NULL, p, dirname, MAXPATHL, TRUE); |
418 /* Only replace it when it starts with '~' */ | 417 // Only replace it when it starts with '~' |
419 if (*dirname == '~') | 418 if (*dirname == '~') |
420 { | 419 { |
421 s = vim_strsave(dirname); | 420 s = vim_strsave(dirname); |
422 if (s != NULL) | 421 if (s != NULL) |
423 { | 422 { |
432 } | 431 } |
433 | 432 |
434 tail = gettail(*fnamep); | 433 tail = gettail(*fnamep); |
435 *fnamelen = (int)STRLEN(*fnamep); | 434 *fnamelen = (int)STRLEN(*fnamep); |
436 | 435 |
437 /* ":h" - head, remove "/file_name", can be repeated */ | 436 // ":h" - head, remove "/file_name", can be repeated |
438 /* Don't remove the first "/" or "c:\" */ | 437 // Don't remove the first "/" or "c:\" |
439 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h') | 438 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h') |
440 { | 439 { |
441 valid |= VALID_HEAD; | 440 valid |= VALID_HEAD; |
442 *usedlen += 2; | 441 *usedlen += 2; |
443 s = get_past_head(*fnamep); | 442 s = get_past_head(*fnamep); |
444 while (tail > s && after_pathsep(s, tail)) | 443 while (tail > s && after_pathsep(s, tail)) |
445 MB_PTR_BACK(*fnamep, tail); | 444 MB_PTR_BACK(*fnamep, tail); |
446 *fnamelen = (int)(tail - *fnamep); | 445 *fnamelen = (int)(tail - *fnamep); |
447 #ifdef VMS | 446 #ifdef VMS |
448 if (*fnamelen > 0) | 447 if (*fnamelen > 0) |
449 *fnamelen += 1; /* the path separator is part of the path */ | 448 *fnamelen += 1; // the path separator is part of the path |
450 #endif | 449 #endif |
451 if (*fnamelen == 0) | 450 if (*fnamelen == 0) |
452 { | 451 { |
453 /* Result is empty. Turn it into "." to make ":cd %:h" work. */ | 452 // Result is empty. Turn it into "." to make ":cd %:h" work. |
454 p = vim_strsave((char_u *)"."); | 453 p = vim_strsave((char_u *)"."); |
455 if (p == NULL) | 454 if (p == NULL) |
456 return -1; | 455 return -1; |
457 vim_free(*bufp); | 456 vim_free(*bufp); |
458 *bufp = *fnamep = tail = p; | 457 *bufp = *fnamep = tail = p; |
463 while (tail > s && !after_pathsep(s, tail)) | 462 while (tail > s && !after_pathsep(s, tail)) |
464 MB_PTR_BACK(*fnamep, tail); | 463 MB_PTR_BACK(*fnamep, tail); |
465 } | 464 } |
466 } | 465 } |
467 | 466 |
468 /* ":8" - shortname */ | 467 // ":8" - shortname |
469 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8') | 468 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8') |
470 { | 469 { |
471 *usedlen += 2; | 470 *usedlen += 2; |
472 #ifdef MSWIN | 471 #ifdef MSWIN |
473 has_shortname = 1; | 472 has_shortname = 1; |
478 /* | 477 /* |
479 * Handle ":8" after we have done 'heads' and before we do 'tails'. | 478 * Handle ":8" after we have done 'heads' and before we do 'tails'. |
480 */ | 479 */ |
481 if (has_shortname) | 480 if (has_shortname) |
482 { | 481 { |
483 /* Copy the string if it is shortened by :h and when it wasn't copied | 482 // Copy the string if it is shortened by :h and when it wasn't copied |
484 * yet, because we are going to change it in place. Avoids changing | 483 // yet, because we are going to change it in place. Avoids changing |
485 * the buffer name for "%:8". */ | 484 // the buffer name for "%:8". |
486 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start) | 485 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start) |
487 { | 486 { |
488 p = vim_strnsave(*fnamep, *fnamelen); | 487 p = vim_strnsave(*fnamep, *fnamelen); |
489 if (p == NULL) | 488 if (p == NULL) |
490 return -1; | 489 return -1; |
491 vim_free(*bufp); | 490 vim_free(*bufp); |
492 *bufp = *fnamep = p; | 491 *bufp = *fnamep = p; |
493 } | 492 } |
494 | 493 |
495 /* Split into two implementations - makes it easier. First is where | 494 // Split into two implementations - makes it easier. First is where |
496 * there isn't a full name already, second is where there is. */ | 495 // there isn't a full name already, second is where there is. |
497 if (!has_fullname && !vim_isAbsName(*fnamep)) | 496 if (!has_fullname && !vim_isAbsName(*fnamep)) |
498 { | 497 { |
499 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL) | 498 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL) |
500 return -1; | 499 return -1; |
501 } | 500 } |
502 else | 501 else |
503 { | 502 { |
504 int l = *fnamelen; | 503 int l = *fnamelen; |
505 | 504 |
506 /* Simple case, already have the full-name. | 505 // Simple case, already have the full-name. |
507 * Nearly always shorter, so try first time. */ | 506 // Nearly always shorter, so try first time. |
508 if (get_short_pathname(fnamep, bufp, &l) == FAIL) | 507 if (get_short_pathname(fnamep, bufp, &l) == FAIL) |
509 return -1; | 508 return -1; |
510 | 509 |
511 if (l == 0) | 510 if (l == 0) |
512 { | 511 { |
513 /* Couldn't find the filename, search the paths. */ | 512 // Couldn't find the filename, search the paths. |
514 l = *fnamelen; | 513 l = *fnamelen; |
515 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL) | 514 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL) |
516 return -1; | 515 return -1; |
517 } | 516 } |
518 *fnamelen = l; | 517 *fnamelen = l; |
519 } | 518 } |
520 } | 519 } |
521 #endif // MSWIN | 520 #endif // MSWIN |
522 | 521 |
523 /* ":t" - tail, just the basename */ | 522 // ":t" - tail, just the basename |
524 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't') | 523 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't') |
525 { | 524 { |
526 *usedlen += 2; | 525 *usedlen += 2; |
527 *fnamelen -= (int)(tail - *fnamep); | 526 *fnamelen -= (int)(tail - *fnamep); |
528 *fnamep = tail; | 527 *fnamep = tail; |
529 } | 528 } |
530 | 529 |
531 /* ":e" - extension, can be repeated */ | 530 // ":e" - extension, can be repeated |
532 /* ":r" - root, without extension, can be repeated */ | 531 // ":r" - root, without extension, can be repeated |
533 while (src[*usedlen] == ':' | 532 while (src[*usedlen] == ':' |
534 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r')) | 533 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r')) |
535 { | 534 { |
536 /* find a '.' in the tail: | 535 // find a '.' in the tail: |
537 * - for second :e: before the current fname | 536 // - for second :e: before the current fname |
538 * - otherwise: The last '.' | 537 // - otherwise: The last '.' |
539 */ | |
540 if (src[*usedlen + 1] == 'e' && *fnamep > tail) | 538 if (src[*usedlen + 1] == 'e' && *fnamep > tail) |
541 s = *fnamep - 2; | 539 s = *fnamep - 2; |
542 else | 540 else |
543 s = *fnamep + *fnamelen - 1; | 541 s = *fnamep + *fnamelen - 1; |
544 for ( ; s > tail; --s) | 542 for ( ; s > tail; --s) |
545 if (s[0] == '.') | 543 if (s[0] == '.') |
546 break; | 544 break; |
547 if (src[*usedlen + 1] == 'e') /* :e */ | 545 if (src[*usedlen + 1] == 'e') // :e |
548 { | 546 { |
549 if (s > tail) | 547 if (s > tail) |
550 { | 548 { |
551 *fnamelen += (int)(*fnamep - (s + 1)); | 549 *fnamelen += (int)(*fnamep - (s + 1)); |
552 *fnamep = s + 1; | 550 *fnamep = s + 1; |
553 #ifdef VMS | 551 #ifdef VMS |
554 /* cut version from the extension */ | 552 // cut version from the extension |
555 s = *fnamep + *fnamelen - 1; | 553 s = *fnamep + *fnamelen - 1; |
556 for ( ; s > *fnamep; --s) | 554 for ( ; s > *fnamep; --s) |
557 if (s[0] == ';') | 555 if (s[0] == ';') |
558 break; | 556 break; |
559 if (s > *fnamep) | 557 if (s > *fnamep) |
561 #endif | 559 #endif |
562 } | 560 } |
563 else if (*fnamep <= tail) | 561 else if (*fnamep <= tail) |
564 *fnamelen = 0; | 562 *fnamelen = 0; |
565 } | 563 } |
566 else /* :r */ | 564 else // :r |
567 { | 565 { |
568 if (s > tail) /* remove one extension */ | 566 if (s > tail) // remove one extension |
569 *fnamelen = (int)(s - *fnamep); | 567 *fnamelen = (int)(s - *fnamep); |
570 } | 568 } |
571 *usedlen += 2; | 569 *usedlen += 2; |
572 } | 570 } |
573 | 571 |
574 /* ":s?pat?foo?" - substitute */ | 572 // ":s?pat?foo?" - substitute |
575 /* ":gs?pat?foo?" - global substitute */ | 573 // ":gs?pat?foo?" - global substitute |
576 if (src[*usedlen] == ':' | 574 if (src[*usedlen] == ':' |
577 && (src[*usedlen + 1] == 's' | 575 && (src[*usedlen + 1] == 's' |
578 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's'))) | 576 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's'))) |
579 { | 577 { |
580 char_u *str; | 578 char_u *str; |
593 } | 591 } |
594 | 592 |
595 sep = *s++; | 593 sep = *s++; |
596 if (sep) | 594 if (sep) |
597 { | 595 { |
598 /* find end of pattern */ | 596 // find end of pattern |
599 p = vim_strchr(s, sep); | 597 p = vim_strchr(s, sep); |
600 if (p != NULL) | 598 if (p != NULL) |
601 { | 599 { |
602 pat = vim_strnsave(s, (int)(p - s)); | 600 pat = vim_strnsave(s, (int)(p - s)); |
603 if (pat != NULL) | 601 if (pat != NULL) |
604 { | 602 { |
605 s = p + 1; | 603 s = p + 1; |
606 /* find end of substitution */ | 604 // find end of substitution |
607 p = vim_strchr(s, sep); | 605 p = vim_strchr(s, sep); |
608 if (p != NULL) | 606 if (p != NULL) |
609 { | 607 { |
610 sub = vim_strnsave(s, (int)(p - s)); | 608 sub = vim_strnsave(s, (int)(p - s)); |
611 str = vim_strnsave(*fnamep, *fnamelen); | 609 str = vim_strnsave(*fnamep, *fnamelen); |
626 vim_free(str); | 624 vim_free(str); |
627 } | 625 } |
628 vim_free(pat); | 626 vim_free(pat); |
629 } | 627 } |
630 } | 628 } |
631 /* after using ":s", repeat all the modifiers */ | 629 // after using ":s", repeat all the modifiers |
632 if (didit) | 630 if (didit) |
633 goto repeat; | 631 goto repeat; |
634 } | 632 } |
635 } | 633 } |
636 | 634 |
637 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S') | 635 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S') |
638 { | 636 { |
639 /* vim_strsave_shellescape() needs a NUL terminated string. */ | 637 // vim_strsave_shellescape() needs a NUL terminated string. |
640 c = (*fnamep)[*fnamelen]; | 638 c = (*fnamep)[*fnamelen]; |
641 if (c != NUL) | 639 if (c != NUL) |
642 (*fnamep)[*fnamelen] = NUL; | 640 (*fnamep)[*fnamelen] = NUL; |
643 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE); | 641 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE); |
644 if (c != NUL) | 642 if (c != NUL) |
720 flags = tv_get_string_buf(&argvars[1], nbuf); | 718 flags = tv_get_string_buf(&argvars[1], nbuf); |
721 else | 719 else |
722 flags = (char_u *)""; | 720 flags = (char_u *)""; |
723 | 721 |
724 if (*flags == NUL) | 722 if (*flags == NUL) |
725 /* delete a file */ | 723 // delete a file |
726 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1; | 724 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1; |
727 else if (STRCMP(flags, "d") == 0) | 725 else if (STRCMP(flags, "d") == 0) |
728 /* delete an empty directory */ | 726 // delete an empty directory |
729 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1; | 727 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1; |
730 else if (STRCMP(flags, "rf") == 0) | 728 else if (STRCMP(flags, "rf") == 0) |
731 /* delete a directory recursively */ | 729 // delete a directory recursively |
732 rettv->vval.v_number = delete_recursive(name); | 730 rettv->vval.v_number = delete_recursive(name); |
733 else | 731 else |
734 semsg(_(e_invexpr2), flags); | 732 semsg(_(e_invexpr2), flags); |
735 } | 733 } |
736 | 734 |
740 void | 738 void |
741 f_executable(typval_T *argvars, typval_T *rettv) | 739 f_executable(typval_T *argvars, typval_T *rettv) |
742 { | 740 { |
743 char_u *name = tv_get_string(&argvars[0]); | 741 char_u *name = tv_get_string(&argvars[0]); |
744 | 742 |
745 /* Check in $PATH and also check directly if there is a directory name. */ | 743 // Check in $PATH and also check directly if there is a directory name. |
746 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE); | 744 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE); |
747 } | 745 } |
748 | 746 |
749 /* | 747 /* |
750 * "exepath()" function | 748 * "exepath()" function |
1020 rettv->vval.v_number = 0; | 1018 rettv->vval.v_number = 0; |
1021 else | 1019 else |
1022 { | 1020 { |
1023 rettv->vval.v_number = (varnumber_T)st.st_size; | 1021 rettv->vval.v_number = (varnumber_T)st.st_size; |
1024 | 1022 |
1025 /* non-perfect check for overflow */ | 1023 // non-perfect check for overflow |
1026 if ((off_T)rettv->vval.v_number != (off_T)st.st_size) | 1024 if ((off_T)rettv->vval.v_number != (off_T)st.st_size) |
1027 rettv->vval.v_number = -2; | 1025 rettv->vval.v_number = -2; |
1028 } | 1026 } |
1029 } | 1027 } |
1030 else | 1028 else |
1093 { | 1091 { |
1094 int options = WILD_SILENT|WILD_USE_NL; | 1092 int options = WILD_SILENT|WILD_USE_NL; |
1095 expand_T xpc; | 1093 expand_T xpc; |
1096 int error = FALSE; | 1094 int error = FALSE; |
1097 | 1095 |
1098 /* When the optional second argument is non-zero, don't remove matches | 1096 // When the optional second argument is non-zero, don't remove matches |
1099 * for 'wildignore' and don't put matches for 'suffixes' at the end. */ | 1097 // for 'wildignore' and don't put matches for 'suffixes' at the end. |
1100 rettv->v_type = VAR_STRING; | 1098 rettv->v_type = VAR_STRING; |
1101 if (argvars[1].v_type != VAR_UNKNOWN) | 1099 if (argvars[1].v_type != VAR_UNKNOWN) |
1102 { | 1100 { |
1103 if (tv_get_number_chk(&argvars[1], &error)) | 1101 if (tv_get_number_chk(&argvars[1], &error)) |
1104 options |= WILD_KEEP_ALL; | 1102 options |= WILD_KEEP_ALL; |
1248 { | 1246 { |
1249 char_u *p; | 1247 char_u *p; |
1250 char_u *updir; | 1248 char_u *updir; |
1251 int r = FAIL; | 1249 int r = FAIL; |
1252 | 1250 |
1253 /* Get end of directory name in "dir". | 1251 // Get end of directory name in "dir". |
1254 * We're done when it's "/" or "c:/". */ | 1252 // We're done when it's "/" or "c:/". |
1255 p = gettail_sep(dir); | 1253 p = gettail_sep(dir); |
1256 if (p <= get_past_head(dir)) | 1254 if (p <= get_past_head(dir)) |
1257 return OK; | 1255 return OK; |
1258 | 1256 |
1259 /* If the directory exists we're done. Otherwise: create it.*/ | 1257 // If the directory exists we're done. Otherwise: create it. |
1260 updir = vim_strnsave(dir, (int)(p - dir)); | 1258 updir = vim_strnsave(dir, (int)(p - dir)); |
1261 if (updir == NULL) | 1259 if (updir == NULL) |
1262 return FAIL; | 1260 return FAIL; |
1263 if (mch_isdir(updir)) | 1261 if (mch_isdir(updir)) |
1264 r = OK; | 1262 r = OK; |
1285 dir = tv_get_string_buf(&argvars[0], buf); | 1283 dir = tv_get_string_buf(&argvars[0], buf); |
1286 if (*dir == NUL) | 1284 if (*dir == NUL) |
1287 return; | 1285 return; |
1288 | 1286 |
1289 if (*gettail(dir) == NUL) | 1287 if (*gettail(dir) == NUL) |
1290 /* remove trailing slashes */ | 1288 // remove trailing slashes |
1291 *gettail_sep(dir) = NUL; | 1289 *gettail_sep(dir) = NUL; |
1292 | 1290 |
1293 if (argvars[1].v_type != VAR_UNKNOWN) | 1291 if (argvars[1].v_type != VAR_UNKNOWN) |
1294 { | 1292 { |
1295 if (argvars[2].v_type != VAR_UNKNOWN) | 1293 if (argvars[2].v_type != VAR_UNKNOWN) |
1300 } | 1298 } |
1301 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0) | 1299 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0) |
1302 { | 1300 { |
1303 if (mch_isdir(dir)) | 1301 if (mch_isdir(dir)) |
1304 { | 1302 { |
1305 /* With the "p" flag it's OK if the dir already exists. */ | 1303 // With the "p" flag it's OK if the dir already exists. |
1306 rettv->vval.v_number = OK; | 1304 rettv->vval.v_number = OK; |
1307 return; | 1305 return; |
1308 } | 1306 } |
1309 mkdir_recurse(dir, prot); | 1307 mkdir_recurse(dir, prot); |
1310 } | 1308 } |
1351 int binary = FALSE; | 1349 int binary = FALSE; |
1352 int blob = FALSE; | 1350 int blob = FALSE; |
1353 int failed = FALSE; | 1351 int failed = FALSE; |
1354 char_u *fname; | 1352 char_u *fname; |
1355 FILE *fd; | 1353 FILE *fd; |
1356 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */ | 1354 char_u buf[(IOSIZE/256)*256]; // rounded to avoid odd + 1 |
1357 int io_size = sizeof(buf); | 1355 int io_size = sizeof(buf); |
1358 int readlen; /* size of last fread() */ | 1356 int readlen; // size of last fread() |
1359 char_u *prev = NULL; /* previously read bytes, if any */ | 1357 char_u *prev = NULL; // previously read bytes, if any |
1360 long prevlen = 0; /* length of data in prev */ | 1358 long prevlen = 0; // length of data in prev |
1361 long prevsize = 0; /* size of prev buffer */ | 1359 long prevsize = 0; // size of prev buffer |
1362 long maxline = MAXLNUM; | 1360 long maxline = MAXLNUM; |
1363 long cnt = 0; | 1361 long cnt = 0; |
1364 char_u *p; /* position in buf */ | 1362 char_u *p; // position in buf |
1365 char_u *start; /* start of current line */ | 1363 char_u *start; // start of current line |
1366 | 1364 |
1367 if (argvars[1].v_type != VAR_UNKNOWN) | 1365 if (argvars[1].v_type != VAR_UNKNOWN) |
1368 { | 1366 { |
1369 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0) | 1367 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0) |
1370 binary = TRUE; | 1368 binary = TRUE; |
1384 { | 1382 { |
1385 if (rettv_list_alloc(rettv) == FAIL) | 1383 if (rettv_list_alloc(rettv) == FAIL) |
1386 return; | 1384 return; |
1387 } | 1385 } |
1388 | 1386 |
1389 /* Always open the file in binary mode, library functions have a mind of | 1387 // Always open the file in binary mode, library functions have a mind of |
1390 * their own about CR-LF conversion. */ | 1388 // their own about CR-LF conversion. |
1391 fname = tv_get_string(&argvars[0]); | 1389 fname = tv_get_string(&argvars[0]); |
1392 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL) | 1390 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL) |
1393 { | 1391 { |
1394 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname); | 1392 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname); |
1395 return; | 1393 return; |
1408 | 1406 |
1409 while (cnt < maxline || maxline < 0) | 1407 while (cnt < maxline || maxline < 0) |
1410 { | 1408 { |
1411 readlen = (int)fread(buf, 1, io_size, fd); | 1409 readlen = (int)fread(buf, 1, io_size, fd); |
1412 | 1410 |
1413 /* This for loop processes what was read, but is also entered at end | 1411 // This for loop processes what was read, but is also entered at end |
1414 * of file so that either: | 1412 // of file so that either: |
1415 * - an incomplete line gets written | 1413 // - an incomplete line gets written |
1416 * - a "binary" file gets an empty line at the end if it ends in a | 1414 // - a "binary" file gets an empty line at the end if it ends in a |
1417 * newline. */ | 1415 // newline. |
1418 for (p = buf, start = buf; | 1416 for (p = buf, start = buf; |
1419 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary)); | 1417 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary)); |
1420 ++p) | 1418 ++p) |
1421 { | 1419 { |
1422 if (*p == '\n' || readlen <= 0) | 1420 if (*p == '\n' || readlen <= 0) |
1423 { | 1421 { |
1424 listitem_T *li; | 1422 listitem_T *li; |
1425 char_u *s = NULL; | 1423 char_u *s = NULL; |
1426 long_u len = p - start; | 1424 long_u len = p - start; |
1427 | 1425 |
1428 /* Finished a line. Remove CRs before NL. */ | 1426 // Finished a line. Remove CRs before NL. |
1429 if (readlen > 0 && !binary) | 1427 if (readlen > 0 && !binary) |
1430 { | 1428 { |
1431 while (len > 0 && start[len - 1] == '\r') | 1429 while (len > 0 && start[len - 1] == '\r') |
1432 --len; | 1430 --len; |
1433 /* removal may cross back to the "prev" string */ | 1431 // removal may cross back to the "prev" string |
1434 if (len == 0) | 1432 if (len == 0) |
1435 while (prevlen > 0 && prev[prevlen - 1] == '\r') | 1433 while (prevlen > 0 && prev[prevlen - 1] == '\r') |
1436 --prevlen; | 1434 --prevlen; |
1437 } | 1435 } |
1438 if (prevlen == 0) | 1436 if (prevlen == 0) |
1439 s = vim_strnsave(start, (int)len); | 1437 s = vim_strnsave(start, (int)len); |
1440 else | 1438 else |
1441 { | 1439 { |
1442 /* Change "prev" buffer to be the right size. This way | 1440 // Change "prev" buffer to be the right size. This way |
1443 * the bytes are only copied once, and very long lines are | 1441 // the bytes are only copied once, and very long lines are |
1444 * allocated only once. */ | 1442 // allocated only once. |
1445 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL) | 1443 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL) |
1446 { | 1444 { |
1447 mch_memmove(s + prevlen, start, len); | 1445 mch_memmove(s + prevlen, start, len); |
1448 s[prevlen + len] = NUL; | 1446 s[prevlen + len] = NUL; |
1449 prev = NULL; /* the list will own the string */ | 1447 prev = NULL; // the list will own the string |
1450 prevlen = prevsize = 0; | 1448 prevlen = prevsize = 0; |
1451 } | 1449 } |
1452 } | 1450 } |
1453 if (s == NULL) | 1451 if (s == NULL) |
1454 { | 1452 { |
1466 li->li_tv.v_type = VAR_STRING; | 1464 li->li_tv.v_type = VAR_STRING; |
1467 li->li_tv.v_lock = 0; | 1465 li->li_tv.v_lock = 0; |
1468 li->li_tv.vval.v_string = s; | 1466 li->li_tv.vval.v_string = s; |
1469 list_append(rettv->vval.v_list, li); | 1467 list_append(rettv->vval.v_list, li); |
1470 | 1468 |
1471 start = p + 1; /* step over newline */ | 1469 start = p + 1; // step over newline |
1472 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0) | 1470 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0) |
1473 break; | 1471 break; |
1474 } | 1472 } |
1475 else if (*p == NUL) | 1473 else if (*p == NUL) |
1476 *p = '\n'; | 1474 *p = '\n'; |
1477 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this | 1475 // Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this |
1478 * when finding the BF and check the previous two bytes. */ | 1476 // when finding the BF and check the previous two bytes. |
1479 else if (*p == 0xbf && enc_utf8 && !binary) | 1477 else if (*p == 0xbf && enc_utf8 && !binary) |
1480 { | 1478 { |
1481 /* Find the two bytes before the 0xbf. If p is at buf, or buf | 1479 // Find the two bytes before the 0xbf. If p is at buf, or buf |
1482 * + 1, these may be in the "prev" string. */ | 1480 // + 1, these may be in the "prev" string. |
1483 char_u back1 = p >= buf + 1 ? p[-1] | 1481 char_u back1 = p >= buf + 1 ? p[-1] |
1484 : prevlen >= 1 ? prev[prevlen - 1] : NUL; | 1482 : prevlen >= 1 ? prev[prevlen - 1] : NUL; |
1485 char_u back2 = p >= buf + 2 ? p[-2] | 1483 char_u back2 = p >= buf + 2 ? p[-2] |
1486 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1] | 1484 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1] |
1487 : prevlen >= 2 ? prev[prevlen - 2] : NUL; | 1485 : prevlen >= 2 ? prev[prevlen - 2] : NUL; |
1488 | 1486 |
1489 if (back2 == 0xef && back1 == 0xbb) | 1487 if (back2 == 0xef && back1 == 0xbb) |
1490 { | 1488 { |
1491 char_u *dest = p - 2; | 1489 char_u *dest = p - 2; |
1492 | 1490 |
1493 /* Usually a BOM is at the beginning of a file, and so at | 1491 // Usually a BOM is at the beginning of a file, and so at |
1494 * the beginning of a line; then we can just step over it. | 1492 // the beginning of a line; then we can just step over it. |
1495 */ | |
1496 if (start == dest) | 1493 if (start == dest) |
1497 start = p + 1; | 1494 start = p + 1; |
1498 else | 1495 else |
1499 { | 1496 { |
1500 /* have to shuffle buf to close gap */ | 1497 // have to shuffle buf to close gap |
1501 int adjust_prevlen = 0; | 1498 int adjust_prevlen = 0; |
1502 | 1499 |
1503 if (dest < buf) | 1500 if (dest < buf) |
1504 { | 1501 { |
1505 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */ | 1502 adjust_prevlen = (int)(buf - dest); // must be 1 or 2 |
1506 dest = buf; | 1503 dest = buf; |
1507 } | 1504 } |
1508 if (readlen > p - buf + 1) | 1505 if (readlen > p - buf + 1) |
1509 mch_memmove(dest, p + 1, readlen - (p - buf) - 1); | 1506 mch_memmove(dest, p + 1, readlen - (p - buf) - 1); |
1510 readlen -= 3 - adjust_prevlen; | 1507 readlen -= 3 - adjust_prevlen; |
1511 prevlen -= adjust_prevlen; | 1508 prevlen -= adjust_prevlen; |
1512 p = dest - 1; | 1509 p = dest - 1; |
1513 } | 1510 } |
1514 } | 1511 } |
1515 } | 1512 } |
1516 } /* for */ | 1513 } // for |
1517 | 1514 |
1518 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0) | 1515 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0) |
1519 break; | 1516 break; |
1520 if (start < p) | 1517 if (start < p) |
1521 { | 1518 { |
1522 /* There's part of a line in buf, store it in "prev". */ | 1519 // There's part of a line in buf, store it in "prev". |
1523 if (p - start + prevlen >= prevsize) | 1520 if (p - start + prevlen >= prevsize) |
1524 { | 1521 { |
1525 /* need bigger "prev" buffer */ | 1522 // need bigger "prev" buffer |
1526 char_u *newprev; | 1523 char_u *newprev; |
1527 | 1524 |
1528 /* A common use case is ordinary text files and "prev" gets a | 1525 // A common use case is ordinary text files and "prev" gets a |
1529 * fragment of a line, so the first allocation is made | 1526 // fragment of a line, so the first allocation is made |
1530 * small, to avoid repeatedly 'allocing' large and | 1527 // small, to avoid repeatedly 'allocing' large and |
1531 * 'reallocing' small. */ | 1528 // 'reallocing' small. |
1532 if (prevsize == 0) | 1529 if (prevsize == 0) |
1533 prevsize = (long)(p - start); | 1530 prevsize = (long)(p - start); |
1534 else | 1531 else |
1535 { | 1532 { |
1536 long grow50pc = (prevsize * 3) / 2; | 1533 long grow50pc = (prevsize * 3) / 2; |
1544 failed = TRUE; | 1541 failed = TRUE; |
1545 break; | 1542 break; |
1546 } | 1543 } |
1547 prev = newprev; | 1544 prev = newprev; |
1548 } | 1545 } |
1549 /* Add the line part to end of "prev". */ | 1546 // Add the line part to end of "prev". |
1550 mch_memmove(prev + prevlen, start, p - start); | 1547 mch_memmove(prev + prevlen, start, p - start); |
1551 prevlen += (long)(p - start); | 1548 prevlen += (long)(p - start); |
1552 } | 1549 } |
1553 } /* while */ | 1550 } // while |
1554 | 1551 |
1555 /* | 1552 // For a negative line count use only the lines at the end of the file, |
1556 * For a negative line count use only the lines at the end of the file, | 1553 // free the rest. |
1557 * free the rest. | |
1558 */ | |
1559 if (!failed && maxline < 0) | 1554 if (!failed && maxline < 0) |
1560 while (cnt > -maxline) | 1555 while (cnt > -maxline) |
1561 { | 1556 { |
1562 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first); | 1557 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first); |
1563 --cnt; | 1558 --cnt; |
1615 | 1610 |
1616 len = STRLEN(p); | 1611 len = STRLEN(p); |
1617 if (len > 0 && after_pathsep(p, p + len)) | 1612 if (len > 0 && after_pathsep(p, p + len)) |
1618 { | 1613 { |
1619 has_trailing_pathsep = TRUE; | 1614 has_trailing_pathsep = TRUE; |
1620 p[len - 1] = NUL; /* the trailing slash breaks readlink() */ | 1615 p[len - 1] = NUL; // the trailing slash breaks readlink() |
1621 } | 1616 } |
1622 | 1617 |
1623 q = getnextcomp(p); | 1618 q = getnextcomp(p); |
1624 if (*q != NUL) | 1619 if (*q != NUL) |
1625 { | 1620 { |
1626 /* Separate the first path component in "p", and keep the | 1621 // Separate the first path component in "p", and keep the |
1627 * remainder (beginning with the path separator). */ | 1622 // remainder (beginning with the path separator). |
1628 remain = vim_strsave(q - 1); | 1623 remain = vim_strsave(q - 1); |
1629 q[-1] = NUL; | 1624 q[-1] = NUL; |
1630 } | 1625 } |
1631 | 1626 |
1632 buf = alloc(MAXPATHL + 1); | 1627 buf = alloc(MAXPATHL + 1); |
1649 emsg(_("E655: Too many symbolic links (cycle?)")); | 1644 emsg(_("E655: Too many symbolic links (cycle?)")); |
1650 rettv->vval.v_string = NULL; | 1645 rettv->vval.v_string = NULL; |
1651 goto fail; | 1646 goto fail; |
1652 } | 1647 } |
1653 | 1648 |
1654 /* Ensure that the result will have a trailing path separator | 1649 // Ensure that the result will have a trailing path separator |
1655 * if the argument has one. */ | 1650 // if the argument has one. |
1656 if (remain == NULL && has_trailing_pathsep) | 1651 if (remain == NULL && has_trailing_pathsep) |
1657 add_pathsep(buf); | 1652 add_pathsep(buf); |
1658 | 1653 |
1659 /* Separate the first path component in the link value and | 1654 // Separate the first path component in the link value and |
1660 * concatenate the remainders. */ | 1655 // concatenate the remainders. |
1661 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf); | 1656 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf); |
1662 if (*q != NUL) | 1657 if (*q != NUL) |
1663 { | 1658 { |
1664 if (remain == NULL) | 1659 if (remain == NULL) |
1665 remain = vim_strsave(q - 1); | 1660 remain = vim_strsave(q - 1); |
1676 } | 1671 } |
1677 | 1672 |
1678 q = gettail(p); | 1673 q = gettail(p); |
1679 if (q > p && *q == NUL) | 1674 if (q > p && *q == NUL) |
1680 { | 1675 { |
1681 /* Ignore trailing path separator. */ | 1676 // Ignore trailing path separator. |
1682 q[-1] = NUL; | 1677 q[-1] = NUL; |
1683 q = gettail(p); | 1678 q = gettail(p); |
1684 } | 1679 } |
1685 if (q > p && !mch_isFullName(buf)) | 1680 if (q > p && !mch_isFullName(buf)) |
1686 { | 1681 { |
1687 /* symlink is relative to directory of argument */ | 1682 // symlink is relative to directory of argument |
1688 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1); | 1683 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1); |
1689 if (cpy != NULL) | 1684 if (cpy != NULL) |
1690 { | 1685 { |
1691 STRCPY(cpy, p); | 1686 STRCPY(cpy, p); |
1692 STRCPY(gettail(cpy), buf); | 1687 STRCPY(gettail(cpy), buf); |
1702 } | 1697 } |
1703 | 1698 |
1704 if (remain == NULL) | 1699 if (remain == NULL) |
1705 break; | 1700 break; |
1706 | 1701 |
1707 /* Append the first path component of "remain" to "p". */ | 1702 // Append the first path component of "remain" to "p". |
1708 q = getnextcomp(remain + 1); | 1703 q = getnextcomp(remain + 1); |
1709 len = q - remain - (*q != NUL); | 1704 len = q - remain - (*q != NUL); |
1710 cpy = vim_strnsave(p, STRLEN(p) + len); | 1705 cpy = vim_strnsave(p, STRLEN(p) + len); |
1711 if (cpy != NULL) | 1706 if (cpy != NULL) |
1712 { | 1707 { |
1713 STRNCAT(cpy, remain, len); | 1708 STRNCAT(cpy, remain, len); |
1714 vim_free(p); | 1709 vim_free(p); |
1715 p = cpy; | 1710 p = cpy; |
1716 } | 1711 } |
1717 /* Shorten "remain". */ | 1712 // Shorten "remain". |
1718 if (*q != NUL) | 1713 if (*q != NUL) |
1719 STRMOVE(remain, q - 1); | 1714 STRMOVE(remain, q - 1); |
1720 else | 1715 else |
1721 VIM_CLEAR(remain); | 1716 VIM_CLEAR(remain); |
1722 } | 1717 } |
1723 | 1718 |
1724 /* If the result is a relative path name, make it explicitly relative to | 1719 // If the result is a relative path name, make it explicitly relative to |
1725 * the current directory if and only if the argument had this form. */ | 1720 // the current directory if and only if the argument had this form. |
1726 if (!vim_ispathsep(*p)) | 1721 if (!vim_ispathsep(*p)) |
1727 { | 1722 { |
1728 if (is_relative_to_current | 1723 if (is_relative_to_current |
1729 && *p != NUL | 1724 && *p != NUL |
1730 && !(p[0] == '.' | 1725 && !(p[0] == '.' |
1732 || vim_ispathsep(p[1]) | 1727 || vim_ispathsep(p[1]) |
1733 || (p[1] == '.' | 1728 || (p[1] == '.' |
1734 && (p[2] == NUL | 1729 && (p[2] == NUL |
1735 || vim_ispathsep(p[2])))))) | 1730 || vim_ispathsep(p[2])))))) |
1736 { | 1731 { |
1737 /* Prepend "./". */ | 1732 // Prepend "./". |
1738 cpy = concat_str((char_u *)"./", p); | 1733 cpy = concat_str((char_u *)"./", p); |
1739 if (cpy != NULL) | 1734 if (cpy != NULL) |
1740 { | 1735 { |
1741 vim_free(p); | 1736 vim_free(p); |
1742 p = cpy; | 1737 p = cpy; |
1743 } | 1738 } |
1744 } | 1739 } |
1745 else if (!is_relative_to_current) | 1740 else if (!is_relative_to_current) |
1746 { | 1741 { |
1747 /* Strip leading "./". */ | 1742 // Strip leading "./". |
1748 q = p; | 1743 q = p; |
1749 while (q[0] == '.' && vim_ispathsep(q[1])) | 1744 while (q[0] == '.' && vim_ispathsep(q[1])) |
1750 q += 2; | 1745 q += 2; |
1751 if (q > p) | 1746 if (q > p) |
1752 STRMOVE(p, p + 2); | 1747 STRMOVE(p, p + 2); |
1753 } | 1748 } |
1754 } | 1749 } |
1755 | 1750 |
1756 /* Ensure that the result will have no trailing path separator | 1751 // Ensure that the result will have no trailing path separator |
1757 * if the argument had none. But keep "/" or "//". */ | 1752 // if the argument had none. But keep "/" or "//". |
1758 if (!has_trailing_pathsep) | 1753 if (!has_trailing_pathsep) |
1759 { | 1754 { |
1760 q = p + STRLEN(p); | 1755 q = p + STRLEN(p); |
1761 if (after_pathsep(p, q)) | 1756 if (after_pathsep(p, q)) |
1762 *gettail_sep(p) = NUL; | 1757 *gettail_sep(p) = NUL; |
1787 static int x = 'A'; | 1782 static int x = 'A'; |
1788 | 1783 |
1789 rettv->v_type = VAR_STRING; | 1784 rettv->v_type = VAR_STRING; |
1790 rettv->vval.v_string = vim_tempname(x, FALSE); | 1785 rettv->vval.v_string = vim_tempname(x, FALSE); |
1791 | 1786 |
1792 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different | 1787 // Advance 'x' to use A-Z and 0-9, so that there are at least 34 different |
1793 * names. Skip 'I' and 'O', they are used for shell redirection. */ | 1788 // names. Skip 'I' and 'O', they are used for shell redirection. |
1794 do | 1789 do |
1795 { | 1790 { |
1796 if (x == 'Z') | 1791 if (x == 'Z') |
1797 x = '0'; | 1792 x = '0'; |
1798 else if (x == '9') | 1793 else if (x == '9') |
1874 | 1869 |
1875 fname = tv_get_string_chk(&argvars[1]); | 1870 fname = tv_get_string_chk(&argvars[1]); |
1876 if (fname == NULL) | 1871 if (fname == NULL) |
1877 return; | 1872 return; |
1878 | 1873 |
1879 /* Always open the file in binary mode, library functions have a mind of | 1874 // Always open the file in binary mode, library functions have a mind of |
1880 * their own about CR-LF conversion. */ | 1875 // their own about CR-LF conversion. |
1881 if (*fname == NUL || (fd = mch_fopen((char *)fname, | 1876 if (*fname == NUL || (fd = mch_fopen((char *)fname, |
1882 append ? APPENDBIN : WRITEBIN)) == NULL) | 1877 append ? APPENDBIN : WRITEBIN)) == NULL) |
1883 { | 1878 { |
1884 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname); | 1879 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname); |
1885 ret = -1; | 1880 ret = -1; |
1900 { | 1895 { |
1901 if (write_list(fd, list, binary) == FAIL) | 1896 if (write_list(fd, list, binary) == FAIL) |
1902 ret = -1; | 1897 ret = -1; |
1903 #ifdef HAVE_FSYNC | 1898 #ifdef HAVE_FSYNC |
1904 else if (do_fsync) | 1899 else if (do_fsync) |
1905 /* Ignore the error, the user wouldn't know what to do about it. | 1900 // Ignore the error, the user wouldn't know what to do about it. |
1906 * May happen for a device. */ | 1901 // May happen for a device. |
1907 vim_ignored = vim_fsync(fileno(fd)); | 1902 vim_ignored = vim_fsync(fileno(fd)); |
1908 #endif | 1903 #endif |
1909 fclose(fd); | 1904 fclose(fd); |
1910 } | 1905 } |
1911 | 1906 |
1919 * Generic browse function. Calls gui_mch_browse() when possible. | 1914 * Generic browse function. Calls gui_mch_browse() when possible. |
1920 * Later this may pop-up a non-GUI file selector (external command?). | 1915 * Later this may pop-up a non-GUI file selector (external command?). |
1921 */ | 1916 */ |
1922 char_u * | 1917 char_u * |
1923 do_browse( | 1918 do_browse( |
1924 int flags, /* BROWSE_SAVE and BROWSE_DIR */ | 1919 int flags, // BROWSE_SAVE and BROWSE_DIR |
1925 char_u *title, /* title for the window */ | 1920 char_u *title, // title for the window |
1926 char_u *dflt, /* default file name (may include directory) */ | 1921 char_u *dflt, // default file name (may include directory) |
1927 char_u *ext, /* extension added */ | 1922 char_u *ext, // extension added |
1928 char_u *initdir, /* initial directory, NULL for current dir or | 1923 char_u *initdir, // initial directory, NULL for current dir or |
1929 when using path from "dflt" */ | 1924 // when using path from "dflt" |
1930 char_u *filter, /* file name filter */ | 1925 char_u *filter, // file name filter |
1931 buf_T *buf) /* buffer to read/write for */ | 1926 buf_T *buf) // buffer to read/write for |
1932 { | 1927 { |
1933 char_u *fname; | 1928 char_u *fname; |
1934 static char_u *last_dir = NULL; /* last used directory */ | 1929 static char_u *last_dir = NULL; // last used directory |
1935 char_u *tofree = NULL; | 1930 char_u *tofree = NULL; |
1936 int save_browse = cmdmod.browse; | 1931 int save_browse = cmdmod.browse; |
1937 | 1932 |
1938 /* Must turn off browse to avoid that autocommands will get the | 1933 // Must turn off browse to avoid that autocommands will get the |
1939 * flag too! */ | 1934 // flag too! |
1940 cmdmod.browse = FALSE; | 1935 cmdmod.browse = FALSE; |
1941 | 1936 |
1942 if (title == NULL || *title == NUL) | 1937 if (title == NULL || *title == NUL) |
1943 { | 1938 { |
1944 if (flags & BROWSE_DIR) | 1939 if (flags & BROWSE_DIR) |
1947 title = (char_u *)_("Save File dialog"); | 1942 title = (char_u *)_("Save File dialog"); |
1948 else | 1943 else |
1949 title = (char_u *)_("Open File dialog"); | 1944 title = (char_u *)_("Open File dialog"); |
1950 } | 1945 } |
1951 | 1946 |
1952 /* When no directory specified, use default file name, default dir, buffer | 1947 // When no directory specified, use default file name, default dir, buffer |
1953 * dir, last dir or current dir */ | 1948 // dir, last dir or current dir |
1954 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL) | 1949 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL) |
1955 { | 1950 { |
1956 if (mch_isdir(dflt)) /* default file name is a directory */ | 1951 if (mch_isdir(dflt)) // default file name is a directory |
1957 { | 1952 { |
1958 initdir = dflt; | 1953 initdir = dflt; |
1959 dflt = NULL; | 1954 dflt = NULL; |
1960 } | 1955 } |
1961 else if (gettail(dflt) != dflt) /* default file name includes a path */ | 1956 else if (gettail(dflt) != dflt) // default file name includes a path |
1962 { | 1957 { |
1963 tofree = vim_strsave(dflt); | 1958 tofree = vim_strsave(dflt); |
1964 if (tofree != NULL) | 1959 if (tofree != NULL) |
1965 { | 1960 { |
1966 initdir = tofree; | 1961 initdir = tofree; |
1970 } | 1965 } |
1971 } | 1966 } |
1972 | 1967 |
1973 if (initdir == NULL || *initdir == NUL) | 1968 if (initdir == NULL || *initdir == NUL) |
1974 { | 1969 { |
1975 /* When 'browsedir' is a directory, use it */ | 1970 // When 'browsedir' is a directory, use it |
1976 if (STRCMP(p_bsdir, "last") != 0 | 1971 if (STRCMP(p_bsdir, "last") != 0 |
1977 && STRCMP(p_bsdir, "buffer") != 0 | 1972 && STRCMP(p_bsdir, "buffer") != 0 |
1978 && STRCMP(p_bsdir, "current") != 0 | 1973 && STRCMP(p_bsdir, "current") != 0 |
1979 && mch_isdir(p_bsdir)) | 1974 && mch_isdir(p_bsdir)) |
1980 initdir = p_bsdir; | 1975 initdir = p_bsdir; |
1981 /* When saving or 'browsedir' is "buffer", use buffer fname */ | 1976 // When saving or 'browsedir' is "buffer", use buffer fname |
1982 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b') | 1977 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b') |
1983 && buf != NULL && buf->b_ffname != NULL) | 1978 && buf != NULL && buf->b_ffname != NULL) |
1984 { | 1979 { |
1985 if (dflt == NULL || *dflt == NUL) | 1980 if (dflt == NULL || *dflt == NUL) |
1986 dflt = gettail(curbuf->b_ffname); | 1981 dflt = gettail(curbuf->b_ffname); |
1989 { | 1984 { |
1990 initdir = tofree; | 1985 initdir = tofree; |
1991 *gettail(initdir) = NUL; | 1986 *gettail(initdir) = NUL; |
1992 } | 1987 } |
1993 } | 1988 } |
1994 /* When 'browsedir' is "last", use dir from last browse */ | 1989 // When 'browsedir' is "last", use dir from last browse |
1995 else if (*p_bsdir == 'l') | 1990 else if (*p_bsdir == 'l') |
1996 initdir = last_dir; | 1991 initdir = last_dir; |
1997 /* When 'browsedir is "current", use current directory. This is the | 1992 // When 'browsedir is "current", use current directory. This is the |
1998 * default already, leave initdir empty. */ | 1993 // default already, leave initdir empty. |
1999 } | 1994 } |
2000 | 1995 |
2001 # ifdef FEAT_GUI | 1996 # ifdef FEAT_GUI |
2002 if (gui.in_use) /* when this changes, also adjust f_has()! */ | 1997 if (gui.in_use) // when this changes, also adjust f_has()! |
2003 { | 1998 { |
2004 if (filter == NULL | 1999 if (filter == NULL |
2005 # ifdef FEAT_EVAL | 2000 # ifdef FEAT_EVAL |
2006 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL | 2001 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL |
2007 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL | 2002 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL |
2009 ) | 2004 ) |
2010 filter = BROWSE_FILTER_DEFAULT; | 2005 filter = BROWSE_FILTER_DEFAULT; |
2011 if (flags & BROWSE_DIR) | 2006 if (flags & BROWSE_DIR) |
2012 { | 2007 { |
2013 # if defined(FEAT_GUI_GTK) || defined(MSWIN) | 2008 # if defined(FEAT_GUI_GTK) || defined(MSWIN) |
2014 /* For systems that have a directory dialog. */ | 2009 // For systems that have a directory dialog. |
2015 fname = gui_mch_browsedir(title, initdir); | 2010 fname = gui_mch_browsedir(title, initdir); |
2016 # else | 2011 # else |
2017 /* Generic solution for selecting a directory: select a file and | 2012 // Generic solution for selecting a directory: select a file and |
2018 * remove the file name. */ | 2013 // remove the file name. |
2019 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)""); | 2014 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)""); |
2020 # endif | 2015 # endif |
2021 # if !defined(FEAT_GUI_GTK) | 2016 # if !defined(FEAT_GUI_GTK) |
2022 /* Win32 adds a dummy file name, others return an arbitrary file | 2017 // Win32 adds a dummy file name, others return an arbitrary file |
2023 * name. GTK+ 2 returns only the directory, */ | 2018 // name. GTK+ 2 returns only the directory, |
2024 if (fname != NULL && *fname != NUL && !mch_isdir(fname)) | 2019 if (fname != NULL && *fname != NUL && !mch_isdir(fname)) |
2025 { | 2020 { |
2026 /* Remove the file name. */ | 2021 // Remove the file name. |
2027 char_u *tail = gettail_sep(fname); | 2022 char_u *tail = gettail_sep(fname); |
2028 | 2023 |
2029 if (tail == fname) | 2024 if (tail == fname) |
2030 *tail++ = '.'; /* use current dir */ | 2025 *tail++ = '.'; // use current dir |
2031 *tail = NUL; | 2026 *tail = NUL; |
2032 } | 2027 } |
2033 # endif | 2028 # endif |
2034 } | 2029 } |
2035 else | 2030 else |
2036 fname = gui_mch_browse(flags & BROWSE_SAVE, | 2031 fname = gui_mch_browse(flags & BROWSE_SAVE, |
2037 title, dflt, ext, initdir, (char_u *)_(filter)); | 2032 title, dflt, ext, initdir, (char_u *)_(filter)); |
2038 | 2033 |
2039 /* We hang around in the dialog for a while, the user might do some | 2034 // We hang around in the dialog for a while, the user might do some |
2040 * things to our files. The Win32 dialog allows deleting or renaming | 2035 // things to our files. The Win32 dialog allows deleting or renaming |
2041 * a file, check timestamps. */ | 2036 // a file, check timestamps. |
2042 need_check_timestamps = TRUE; | 2037 need_check_timestamps = TRUE; |
2043 did_check_timestamps = FALSE; | 2038 did_check_timestamps = FALSE; |
2044 } | 2039 } |
2045 else | 2040 else |
2046 # endif | 2041 # endif |
2047 { | 2042 { |
2048 /* TODO: non-GUI file selector here */ | 2043 // TODO: non-GUI file selector here |
2049 emsg(_("E338: Sorry, no file browser in console mode")); | 2044 emsg(_("E338: Sorry, no file browser in console mode")); |
2050 fname = NULL; | 2045 fname = NULL; |
2051 } | 2046 } |
2052 | 2047 |
2053 /* keep the directory for next time */ | 2048 // keep the directory for next time |
2054 if (fname != NULL) | 2049 if (fname != NULL) |
2055 { | 2050 { |
2056 vim_free(last_dir); | 2051 vim_free(last_dir); |
2057 last_dir = vim_strsave(fname); | 2052 last_dir = vim_strsave(fname); |
2058 if (last_dir != NULL && !(flags & BROWSE_DIR)) | 2053 if (last_dir != NULL && !(flags & BROWSE_DIR)) |
2059 { | 2054 { |
2060 *gettail(last_dir) = NUL; | 2055 *gettail(last_dir) = NUL; |
2061 if (*last_dir == NUL) | 2056 if (*last_dir == NUL) |
2062 { | 2057 { |
2063 /* filename only returned, must be in current dir */ | 2058 // filename only returned, must be in current dir |
2064 vim_free(last_dir); | 2059 vim_free(last_dir); |
2065 last_dir = alloc(MAXPATHL); | 2060 last_dir = alloc(MAXPATHL); |
2066 if (last_dir != NULL) | 2061 if (last_dir != NULL) |
2067 mch_dirname(last_dir, MAXPATHL); | 2062 mch_dirname(last_dir, MAXPATHL); |
2068 } | 2063 } |
2134 # endif | 2129 # endif |
2135 rettv->v_type = VAR_STRING; | 2130 rettv->v_type = VAR_STRING; |
2136 } | 2131 } |
2137 | 2132 |
2138 #endif // FEAT_EVAL | 2133 #endif // FEAT_EVAL |
2134 | |
2135 /* | |
2136 * Replace home directory by "~" in each space or comma separated file name in | |
2137 * 'src'. | |
2138 * If anything fails (except when out of space) dst equals src. | |
2139 */ | |
2140 void | |
2141 home_replace( | |
2142 buf_T *buf, // when not NULL, check for help files | |
2143 char_u *src, // input file name | |
2144 char_u *dst, // where to put the result | |
2145 int dstlen, // maximum length of the result | |
2146 int one) // if TRUE, only replace one file name, include | |
2147 // spaces and commas in the file name. | |
2148 { | |
2149 size_t dirlen = 0, envlen = 0; | |
2150 size_t len; | |
2151 char_u *homedir_env, *homedir_env_orig; | |
2152 char_u *p; | |
2153 | |
2154 if (src == NULL) | |
2155 { | |
2156 *dst = NUL; | |
2157 return; | |
2158 } | |
2159 | |
2160 /* | |
2161 * If the file is a help file, remove the path completely. | |
2162 */ | |
2163 if (buf != NULL && buf->b_help) | |
2164 { | |
2165 vim_snprintf((char *)dst, dstlen, "%s", gettail(src)); | |
2166 return; | |
2167 } | |
2168 | |
2169 /* | |
2170 * We check both the value of the $HOME environment variable and the | |
2171 * "real" home directory. | |
2172 */ | |
2173 if (homedir != NULL) | |
2174 dirlen = STRLEN(homedir); | |
2175 | |
2176 #ifdef VMS | |
2177 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN"); | |
2178 #else | |
2179 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME"); | |
2180 #endif | |
2181 #ifdef MSWIN | |
2182 if (homedir_env == NULL) | |
2183 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE"); | |
2184 #endif | |
2185 // Empty is the same as not set. | |
2186 if (homedir_env != NULL && *homedir_env == NUL) | |
2187 homedir_env = NULL; | |
2188 | |
2189 if (homedir_env != NULL && *homedir_env == '~') | |
2190 { | |
2191 int usedlen = 0; | |
2192 int flen; | |
2193 char_u *fbuf = NULL; | |
2194 | |
2195 flen = (int)STRLEN(homedir_env); | |
2196 (void)modify_fname((char_u *)":p", FALSE, &usedlen, | |
2197 &homedir_env, &fbuf, &flen); | |
2198 flen = (int)STRLEN(homedir_env); | |
2199 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1])) | |
2200 // Remove the trailing / that is added to a directory. | |
2201 homedir_env[flen - 1] = NUL; | |
2202 } | |
2203 | |
2204 if (homedir_env != NULL) | |
2205 envlen = STRLEN(homedir_env); | |
2206 | |
2207 if (!one) | |
2208 src = skipwhite(src); | |
2209 while (*src && dstlen > 0) | |
2210 { | |
2211 /* | |
2212 * Here we are at the beginning of a file name. | |
2213 * First, check to see if the beginning of the file name matches | |
2214 * $HOME or the "real" home directory. Check that there is a '/' | |
2215 * after the match (so that if e.g. the file is "/home/pieter/bla", | |
2216 * and the home directory is "/home/piet", the file does not end up | |
2217 * as "~er/bla" (which would seem to indicate the file "bla" in user | |
2218 * er's home directory)). | |
2219 */ | |
2220 p = homedir; | |
2221 len = dirlen; | |
2222 for (;;) | |
2223 { | |
2224 if ( len | |
2225 && fnamencmp(src, p, len) == 0 | |
2226 && (vim_ispathsep(src[len]) | |
2227 || (!one && (src[len] == ',' || src[len] == ' ')) | |
2228 || src[len] == NUL)) | |
2229 { | |
2230 src += len; | |
2231 if (--dstlen > 0) | |
2232 *dst++ = '~'; | |
2233 | |
2234 /* | |
2235 * If it's just the home directory, add "/". | |
2236 */ | |
2237 if (!vim_ispathsep(src[0]) && --dstlen > 0) | |
2238 *dst++ = '/'; | |
2239 break; | |
2240 } | |
2241 if (p == homedir_env) | |
2242 break; | |
2243 p = homedir_env; | |
2244 len = envlen; | |
2245 } | |
2246 | |
2247 // if (!one) skip to separator: space or comma | |
2248 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0) | |
2249 *dst++ = *src++; | |
2250 // skip separator | |
2251 while ((*src == ' ' || *src == ',') && --dstlen > 0) | |
2252 *dst++ = *src++; | |
2253 } | |
2254 // if (dstlen == 0) out of space, what to do??? | |
2255 | |
2256 *dst = NUL; | |
2257 | |
2258 if (homedir_env != homedir_env_orig) | |
2259 vim_free(homedir_env); | |
2260 } | |
2261 | |
2262 /* | |
2263 * Like home_replace, store the replaced string in allocated memory. | |
2264 * When something fails, NULL is returned. | |
2265 */ | |
2266 char_u * | |
2267 home_replace_save( | |
2268 buf_T *buf, // when not NULL, check for help files | |
2269 char_u *src) // input file name | |
2270 { | |
2271 char_u *dst; | |
2272 unsigned len; | |
2273 | |
2274 len = 3; // space for "~/" and trailing NUL | |
2275 if (src != NULL) // just in case | |
2276 len += (unsigned)STRLEN(src); | |
2277 dst = alloc(len); | |
2278 if (dst != NULL) | |
2279 home_replace(buf, src, dst, len, TRUE); | |
2280 return dst; | |
2281 } | |
2282 | |
2283 /* | |
2284 * Compare two file names and return: | |
2285 * FPC_SAME if they both exist and are the same file. | |
2286 * FPC_SAMEX if they both don't exist and have the same file name. | |
2287 * FPC_DIFF if they both exist and are different files. | |
2288 * FPC_NOTX if they both don't exist. | |
2289 * FPC_DIFFX if one of them doesn't exist. | |
2290 * For the first name environment variables are expanded if "expandenv" is | |
2291 * TRUE. | |
2292 */ | |
2293 int | |
2294 fullpathcmp( | |
2295 char_u *s1, | |
2296 char_u *s2, | |
2297 int checkname, // when both don't exist, check file names | |
2298 int expandenv) | |
2299 { | |
2300 #ifdef UNIX | |
2301 char_u exp1[MAXPATHL]; | |
2302 char_u full1[MAXPATHL]; | |
2303 char_u full2[MAXPATHL]; | |
2304 stat_T st1, st2; | |
2305 int r1, r2; | |
2306 | |
2307 if (expandenv) | |
2308 expand_env(s1, exp1, MAXPATHL); | |
2309 else | |
2310 vim_strncpy(exp1, s1, MAXPATHL - 1); | |
2311 r1 = mch_stat((char *)exp1, &st1); | |
2312 r2 = mch_stat((char *)s2, &st2); | |
2313 if (r1 != 0 && r2 != 0) | |
2314 { | |
2315 /* if mch_stat() doesn't work, may compare the names */ | |
2316 if (checkname) | |
2317 { | |
2318 if (fnamecmp(exp1, s2) == 0) | |
2319 return FPC_SAMEX; | |
2320 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); | |
2321 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); | |
2322 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0) | |
2323 return FPC_SAMEX; | |
2324 } | |
2325 return FPC_NOTX; | |
2326 } | |
2327 if (r1 != 0 || r2 != 0) | |
2328 return FPC_DIFFX; | |
2329 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) | |
2330 return FPC_SAME; | |
2331 return FPC_DIFF; | |
2332 #else | |
2333 char_u *exp1; // expanded s1 | |
2334 char_u *full1; // full path of s1 | |
2335 char_u *full2; // full path of s2 | |
2336 int retval = FPC_DIFF; | |
2337 int r1, r2; | |
2338 | |
2339 // allocate one buffer to store three paths (alloc()/free() is slow!) | |
2340 if ((exp1 = alloc(MAXPATHL * 3)) != NULL) | |
2341 { | |
2342 full1 = exp1 + MAXPATHL; | |
2343 full2 = full1 + MAXPATHL; | |
2344 | |
2345 if (expandenv) | |
2346 expand_env(s1, exp1, MAXPATHL); | |
2347 else | |
2348 vim_strncpy(exp1, s1, MAXPATHL - 1); | |
2349 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE); | |
2350 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE); | |
2351 | |
2352 // If vim_FullName() fails, the file probably doesn't exist. | |
2353 if (r1 != OK && r2 != OK) | |
2354 { | |
2355 if (checkname && fnamecmp(exp1, s2) == 0) | |
2356 retval = FPC_SAMEX; | |
2357 else | |
2358 retval = FPC_NOTX; | |
2359 } | |
2360 else if (r1 != OK || r2 != OK) | |
2361 retval = FPC_DIFFX; | |
2362 else if (fnamecmp(full1, full2)) | |
2363 retval = FPC_DIFF; | |
2364 else | |
2365 retval = FPC_SAME; | |
2366 vim_free(exp1); | |
2367 } | |
2368 return retval; | |
2369 #endif | |
2370 } | |
2371 | |
2372 /* | |
2373 * Get the tail of a path: the file name. | |
2374 * When the path ends in a path separator the tail is the NUL after it. | |
2375 * Fail safe: never returns NULL. | |
2376 */ | |
2377 char_u * | |
2378 gettail(char_u *fname) | |
2379 { | |
2380 char_u *p1, *p2; | |
2381 | |
2382 if (fname == NULL) | |
2383 return (char_u *)""; | |
2384 for (p1 = p2 = get_past_head(fname); *p2; ) // find last part of path | |
2385 { | |
2386 if (vim_ispathsep_nocolon(*p2)) | |
2387 p1 = p2 + 1; | |
2388 MB_PTR_ADV(p2); | |
2389 } | |
2390 return p1; | |
2391 } | |
2392 | |
2393 /* | |
2394 * Get pointer to tail of "fname", including path separators. Putting a NUL | |
2395 * here leaves the directory name. Takes care of "c:/" and "//". | |
2396 * Always returns a valid pointer. | |
2397 */ | |
2398 char_u * | |
2399 gettail_sep(char_u *fname) | |
2400 { | |
2401 char_u *p; | |
2402 char_u *t; | |
2403 | |
2404 p = get_past_head(fname); // don't remove the '/' from "c:/file" | |
2405 t = gettail(fname); | |
2406 while (t > p && after_pathsep(fname, t)) | |
2407 --t; | |
2408 #ifdef VMS | |
2409 // path separator is part of the path | |
2410 ++t; | |
2411 #endif | |
2412 return t; | |
2413 } | |
2414 | |
2415 /* | |
2416 * get the next path component (just after the next path separator). | |
2417 */ | |
2418 char_u * | |
2419 getnextcomp(char_u *fname) | |
2420 { | |
2421 while (*fname && !vim_ispathsep(*fname)) | |
2422 MB_PTR_ADV(fname); | |
2423 if (*fname) | |
2424 ++fname; | |
2425 return fname; | |
2426 } | |
2427 | |
2428 /* | |
2429 * Get a pointer to one character past the head of a path name. | |
2430 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head. | |
2431 * If there is no head, path is returned. | |
2432 */ | |
2433 char_u * | |
2434 get_past_head(char_u *path) | |
2435 { | |
2436 char_u *retval; | |
2437 | |
2438 #if defined(MSWIN) | |
2439 // may skip "c:" | |
2440 if (isalpha(path[0]) && path[1] == ':') | |
2441 retval = path + 2; | |
2442 else | |
2443 retval = path; | |
2444 #else | |
2445 # if defined(AMIGA) | |
2446 // may skip "label:" | |
2447 retval = vim_strchr(path, ':'); | |
2448 if (retval == NULL) | |
2449 retval = path; | |
2450 # else // Unix | |
2451 retval = path; | |
2452 # endif | |
2453 #endif | |
2454 | |
2455 while (vim_ispathsep(*retval)) | |
2456 ++retval; | |
2457 | |
2458 return retval; | |
2459 } | |
2460 | |
2461 /* | |
2462 * Return TRUE if 'c' is a path separator. | |
2463 * Note that for MS-Windows this includes the colon. | |
2464 */ | |
2465 int | |
2466 vim_ispathsep(int c) | |
2467 { | |
2468 #ifdef UNIX | |
2469 return (c == '/'); // UNIX has ':' inside file names | |
2470 #else | |
2471 # ifdef BACKSLASH_IN_FILENAME | |
2472 return (c == ':' || c == '/' || c == '\\'); | |
2473 # else | |
2474 # ifdef VMS | |
2475 // server"user passwd"::device:[full.path.name]fname.extension;version" | |
2476 return (c == ':' || c == '[' || c == ']' || c == '/' | |
2477 || c == '<' || c == '>' || c == '"' ); | |
2478 # else | |
2479 return (c == ':' || c == '/'); | |
2480 # endif // VMS | |
2481 # endif | |
2482 #endif | |
2483 } | |
2484 | |
2485 /* | |
2486 * Like vim_ispathsep(c), but exclude the colon for MS-Windows. | |
2487 */ | |
2488 int | |
2489 vim_ispathsep_nocolon(int c) | |
2490 { | |
2491 return vim_ispathsep(c) | |
2492 #ifdef BACKSLASH_IN_FILENAME | |
2493 && c != ':' | |
2494 #endif | |
2495 ; | |
2496 } | |
2497 | |
2498 /* | |
2499 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" | |
2500 * It's done in-place. | |
2501 */ | |
2502 void | |
2503 shorten_dir(char_u *str) | |
2504 { | |
2505 char_u *tail, *s, *d; | |
2506 int skip = FALSE; | |
2507 | |
2508 tail = gettail(str); | |
2509 d = str; | |
2510 for (s = str; ; ++s) | |
2511 { | |
2512 if (s >= tail) // copy the whole tail | |
2513 { | |
2514 *d++ = *s; | |
2515 if (*s == NUL) | |
2516 break; | |
2517 } | |
2518 else if (vim_ispathsep(*s)) // copy '/' and next char | |
2519 { | |
2520 *d++ = *s; | |
2521 skip = FALSE; | |
2522 } | |
2523 else if (!skip) | |
2524 { | |
2525 *d++ = *s; // copy next char | |
2526 if (*s != '~' && *s != '.') // and leading "~" and "." | |
2527 skip = TRUE; | |
2528 if (has_mbyte) | |
2529 { | |
2530 int l = mb_ptr2len(s); | |
2531 | |
2532 while (--l > 0) | |
2533 *d++ = *++s; | |
2534 } | |
2535 } | |
2536 } | |
2537 } | |
2538 | |
2539 /* | |
2540 * Return TRUE if the directory of "fname" exists, FALSE otherwise. | |
2541 * Also returns TRUE if there is no directory name. | |
2542 * "fname" must be writable!. | |
2543 */ | |
2544 int | |
2545 dir_of_file_exists(char_u *fname) | |
2546 { | |
2547 char_u *p; | |
2548 int c; | |
2549 int retval; | |
2550 | |
2551 p = gettail_sep(fname); | |
2552 if (p == fname) | |
2553 return TRUE; | |
2554 c = *p; | |
2555 *p = NUL; | |
2556 retval = mch_isdir(fname); | |
2557 *p = c; | |
2558 return retval; | |
2559 } | |
2560 | |
2561 /* | |
2562 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally | |
2563 * and deal with 'fileignorecase'. | |
2564 */ | |
2565 int | |
2566 vim_fnamecmp(char_u *x, char_u *y) | |
2567 { | |
2568 #ifdef BACKSLASH_IN_FILENAME | |
2569 return vim_fnamencmp(x, y, MAXPATHL); | |
2570 #else | |
2571 if (p_fic) | |
2572 return MB_STRICMP(x, y); | |
2573 return STRCMP(x, y); | |
2574 #endif | |
2575 } | |
2576 | |
2577 int | |
2578 vim_fnamencmp(char_u *x, char_u *y, size_t len) | |
2579 { | |
2580 #ifdef BACKSLASH_IN_FILENAME | |
2581 char_u *px = x; | |
2582 char_u *py = y; | |
2583 int cx = NUL; | |
2584 int cy = NUL; | |
2585 | |
2586 while (len > 0) | |
2587 { | |
2588 cx = PTR2CHAR(px); | |
2589 cy = PTR2CHAR(py); | |
2590 if (cx == NUL || cy == NUL | |
2591 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy) | |
2592 && !(cx == '/' && cy == '\\') | |
2593 && !(cx == '\\' && cy == '/'))) | |
2594 break; | |
2595 len -= MB_PTR2LEN(px); | |
2596 px += MB_PTR2LEN(px); | |
2597 py += MB_PTR2LEN(py); | |
2598 } | |
2599 if (len == 0) | |
2600 return 0; | |
2601 return (cx - cy); | |
2602 #else | |
2603 if (p_fic) | |
2604 return MB_STRNICMP(x, y, len); | |
2605 return STRNCMP(x, y, len); | |
2606 #endif | |
2607 } | |
2608 | |
2609 /* | |
2610 * Concatenate file names fname1 and fname2 into allocated memory. | |
2611 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary. | |
2612 */ | |
2613 char_u * | |
2614 concat_fnames(char_u *fname1, char_u *fname2, int sep) | |
2615 { | |
2616 char_u *dest; | |
2617 | |
2618 dest = alloc(STRLEN(fname1) + STRLEN(fname2) + 3); | |
2619 if (dest != NULL) | |
2620 { | |
2621 STRCPY(dest, fname1); | |
2622 if (sep) | |
2623 add_pathsep(dest); | |
2624 STRCAT(dest, fname2); | |
2625 } | |
2626 return dest; | |
2627 } | |
2628 | |
2629 /* | |
2630 * Add a path separator to a file name, unless it already ends in a path | |
2631 * separator. | |
2632 */ | |
2633 void | |
2634 add_pathsep(char_u *p) | |
2635 { | |
2636 if (*p != NUL && !after_pathsep(p, p + STRLEN(p))) | |
2637 STRCAT(p, PATHSEPSTR); | |
2638 } | |
2639 | |
2640 /* | |
2641 * FullName_save - Make an allocated copy of a full file name. | |
2642 * Returns NULL when out of memory. | |
2643 */ | |
2644 char_u * | |
2645 FullName_save( | |
2646 char_u *fname, | |
2647 int force) // force expansion, even when it already looks | |
2648 // like a full path name | |
2649 { | |
2650 char_u *buf; | |
2651 char_u *new_fname = NULL; | |
2652 | |
2653 if (fname == NULL) | |
2654 return NULL; | |
2655 | |
2656 buf = alloc(MAXPATHL); | |
2657 if (buf != NULL) | |
2658 { | |
2659 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL) | |
2660 new_fname = vim_strsave(buf); | |
2661 else | |
2662 new_fname = vim_strsave(fname); | |
2663 vim_free(buf); | |
2664 } | |
2665 return new_fname; | |
2666 } | |
2667 | |
2668 /* | |
2669 * return TRUE if "fname" exists. | |
2670 */ | |
2671 int | |
2672 vim_fexists(char_u *fname) | |
2673 { | |
2674 stat_T st; | |
2675 | |
2676 if (mch_stat((char *)fname, &st)) | |
2677 return FALSE; | |
2678 return TRUE; | |
2679 } | |
2680 | |
2681 /* | |
2682 * Invoke expand_wildcards() for one pattern. | |
2683 * Expand items like "%:h" before the expansion. | |
2684 * Returns OK or FAIL. | |
2685 */ | |
2686 int | |
2687 expand_wildcards_eval( | |
2688 char_u **pat, // pointer to input pattern | |
2689 int *num_file, // resulting number of files | |
2690 char_u ***file, // array of resulting files | |
2691 int flags) // EW_DIR, etc. | |
2692 { | |
2693 int ret = FAIL; | |
2694 char_u *eval_pat = NULL; | |
2695 char_u *exp_pat = *pat; | |
2696 char *ignored_msg; | |
2697 int usedlen; | |
2698 | |
2699 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<') | |
2700 { | |
2701 ++emsg_off; | |
2702 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen, | |
2703 NULL, &ignored_msg, NULL); | |
2704 --emsg_off; | |
2705 if (eval_pat != NULL) | |
2706 exp_pat = concat_str(eval_pat, exp_pat + usedlen); | |
2707 } | |
2708 | |
2709 if (exp_pat != NULL) | |
2710 ret = expand_wildcards(1, &exp_pat, num_file, file, flags); | |
2711 | |
2712 if (eval_pat != NULL) | |
2713 { | |
2714 vim_free(exp_pat); | |
2715 vim_free(eval_pat); | |
2716 } | |
2717 | |
2718 return ret; | |
2719 } | |
2720 | |
2721 /* | |
2722 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching | |
2723 * 'wildignore'. | |
2724 * Returns OK or FAIL. When FAIL then "num_files" won't be set. | |
2725 */ | |
2726 int | |
2727 expand_wildcards( | |
2728 int num_pat, // number of input patterns | |
2729 char_u **pat, // array of input patterns | |
2730 int *num_files, // resulting number of files | |
2731 char_u ***files, // array of resulting files | |
2732 int flags) // EW_DIR, etc. | |
2733 { | |
2734 int retval; | |
2735 int i, j; | |
2736 char_u *p; | |
2737 int non_suf_match; // number without matching suffix | |
2738 | |
2739 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags); | |
2740 | |
2741 // When keeping all matches, return here | |
2742 if ((flags & EW_KEEPALL) || retval == FAIL) | |
2743 return retval; | |
2744 | |
2745 #ifdef FEAT_WILDIGN | |
2746 /* | |
2747 * Remove names that match 'wildignore'. | |
2748 */ | |
2749 if (*p_wig) | |
2750 { | |
2751 char_u *ffname; | |
2752 | |
2753 // check all files in (*files)[] | |
2754 for (i = 0; i < *num_files; ++i) | |
2755 { | |
2756 ffname = FullName_save((*files)[i], FALSE); | |
2757 if (ffname == NULL) // out of memory | |
2758 break; | |
2759 # ifdef VMS | |
2760 vms_remove_version(ffname); | |
2761 # endif | |
2762 if (match_file_list(p_wig, (*files)[i], ffname)) | |
2763 { | |
2764 // remove this matching file from the list | |
2765 vim_free((*files)[i]); | |
2766 for (j = i; j + 1 < *num_files; ++j) | |
2767 (*files)[j] = (*files)[j + 1]; | |
2768 --*num_files; | |
2769 --i; | |
2770 } | |
2771 vim_free(ffname); | |
2772 } | |
2773 | |
2774 // If the number of matches is now zero, we fail. | |
2775 if (*num_files == 0) | |
2776 { | |
2777 VIM_CLEAR(*files); | |
2778 return FAIL; | |
2779 } | |
2780 } | |
2781 #endif | |
2782 | |
2783 /* | |
2784 * Move the names where 'suffixes' match to the end. | |
2785 */ | |
2786 if (*num_files > 1) | |
2787 { | |
2788 non_suf_match = 0; | |
2789 for (i = 0; i < *num_files; ++i) | |
2790 { | |
2791 if (!match_suffix((*files)[i])) | |
2792 { | |
2793 /* | |
2794 * Move the name without matching suffix to the front | |
2795 * of the list. | |
2796 */ | |
2797 p = (*files)[i]; | |
2798 for (j = i; j > non_suf_match; --j) | |
2799 (*files)[j] = (*files)[j - 1]; | |
2800 (*files)[non_suf_match++] = p; | |
2801 } | |
2802 } | |
2803 } | |
2804 | |
2805 return retval; | |
2806 } | |
2807 | |
2808 /* | |
2809 * Return TRUE if "fname" matches with an entry in 'suffixes'. | |
2810 */ | |
2811 int | |
2812 match_suffix(char_u *fname) | |
2813 { | |
2814 int fnamelen, setsuflen; | |
2815 char_u *setsuf; | |
2816 #define MAXSUFLEN 30 // maximum length of a file suffix | |
2817 char_u suf_buf[MAXSUFLEN]; | |
2818 | |
2819 fnamelen = (int)STRLEN(fname); | |
2820 setsuflen = 0; | |
2821 for (setsuf = p_su; *setsuf; ) | |
2822 { | |
2823 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,"); | |
2824 if (setsuflen == 0) | |
2825 { | |
2826 char_u *tail = gettail(fname); | |
2827 | |
2828 // empty entry: match name without a '.' | |
2829 if (vim_strchr(tail, '.') == NULL) | |
2830 { | |
2831 setsuflen = 1; | |
2832 break; | |
2833 } | |
2834 } | |
2835 else | |
2836 { | |
2837 if (fnamelen >= setsuflen | |
2838 && fnamencmp(suf_buf, fname + fnamelen - setsuflen, | |
2839 (size_t)setsuflen) == 0) | |
2840 break; | |
2841 setsuflen = 0; | |
2842 } | |
2843 } | |
2844 return (setsuflen != 0); | |
2845 } | |
2846 | |
2847 #ifdef VIM_BACKTICK | |
2848 | |
2849 /* | |
2850 * Return TRUE if we can expand this backtick thing here. | |
2851 */ | |
2852 static int | |
2853 vim_backtick(char_u *p) | |
2854 { | |
2855 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`'); | |
2856 } | |
2857 | |
2858 /* | |
2859 * Expand an item in `backticks` by executing it as a command. | |
2860 * Currently only works when pat[] starts and ends with a `. | |
2861 * Returns number of file names found, -1 if an error is encountered. | |
2862 */ | |
2863 static int | |
2864 expand_backtick( | |
2865 garray_T *gap, | |
2866 char_u *pat, | |
2867 int flags) // EW_* flags | |
2868 { | |
2869 char_u *p; | |
2870 char_u *cmd; | |
2871 char_u *buffer; | |
2872 int cnt = 0; | |
2873 int i; | |
2874 | |
2875 // Create the command: lop off the backticks. | |
2876 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2); | |
2877 if (cmd == NULL) | |
2878 return -1; | |
2879 | |
2880 #ifdef FEAT_EVAL | |
2881 if (*cmd == '=') // `={expr}`: Expand expression | |
2882 buffer = eval_to_string(cmd + 1, &p, TRUE); | |
2883 else | |
2884 #endif | |
2885 buffer = get_cmd_output(cmd, NULL, | |
2886 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL); | |
2887 vim_free(cmd); | |
2888 if (buffer == NULL) | |
2889 return -1; | |
2890 | |
2891 cmd = buffer; | |
2892 while (*cmd != NUL) | |
2893 { | |
2894 cmd = skipwhite(cmd); // skip over white space | |
2895 p = cmd; | |
2896 while (*p != NUL && *p != '\r' && *p != '\n') // skip over entry | |
2897 ++p; | |
2898 // add an entry if it is not empty | |
2899 if (p > cmd) | |
2900 { | |
2901 i = *p; | |
2902 *p = NUL; | |
2903 addfile(gap, cmd, flags); | |
2904 *p = i; | |
2905 ++cnt; | |
2906 } | |
2907 cmd = p; | |
2908 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n')) | |
2909 ++cmd; | |
2910 } | |
2911 | |
2912 vim_free(buffer); | |
2913 return cnt; | |
2914 } | |
2915 #endif // VIM_BACKTICK | |
2916 | |
2917 #if defined(MSWIN) | |
2918 /* | |
2919 * File name expansion code for MS-DOS, Win16 and Win32. It's here because | |
2920 * it's shared between these systems. | |
2921 */ | |
2922 | |
2923 /* | |
2924 * comparison function for qsort in dos_expandpath() | |
2925 */ | |
2926 static int | |
2927 pstrcmp(const void *a, const void *b) | |
2928 { | |
2929 return (pathcmp(*(char **)a, *(char **)b, -1)); | |
2930 } | |
2931 | |
2932 /* | |
2933 * Recursively expand one path component into all matching files and/or | |
2934 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. | |
2935 * Return the number of matches found. | |
2936 * "path" has backslashes before chars that are not to be expanded, starting | |
2937 * at "path[wildoff]". | |
2938 * Return the number of matches found. | |
2939 * NOTE: much of this is identical to unix_expandpath(), keep in sync! | |
2940 */ | |
2941 static int | |
2942 dos_expandpath( | |
2943 garray_T *gap, | |
2944 char_u *path, | |
2945 int wildoff, | |
2946 int flags, // EW_* flags | |
2947 int didstar) // expanded "**" once already | |
2948 { | |
2949 char_u *buf; | |
2950 char_u *path_end; | |
2951 char_u *p, *s, *e; | |
2952 int start_len = gap->ga_len; | |
2953 char_u *pat; | |
2954 regmatch_T regmatch; | |
2955 int starts_with_dot; | |
2956 int matches; | |
2957 int len; | |
2958 int starstar = FALSE; | |
2959 static int stardepth = 0; // depth for "**" expansion | |
2960 HANDLE hFind = INVALID_HANDLE_VALUE; | |
2961 WIN32_FIND_DATAW wfb; | |
2962 WCHAR *wn = NULL; // UCS-2 name, NULL when not used. | |
2963 char_u *matchname; | |
2964 int ok; | |
2965 | |
2966 // Expanding "**" may take a long time, check for CTRL-C. | |
2967 if (stardepth > 0) | |
2968 { | |
2969 ui_breakcheck(); | |
2970 if (got_int) | |
2971 return 0; | |
2972 } | |
2973 | |
2974 // Make room for file name. When doing encoding conversion the actual | |
2975 // length may be quite a bit longer, thus use the maximum possible length. | |
2976 buf = alloc(MAXPATHL); | |
2977 if (buf == NULL) | |
2978 return 0; | |
2979 | |
2980 /* | |
2981 * Find the first part in the path name that contains a wildcard or a ~1. | |
2982 * Copy it into buf, including the preceding characters. | |
2983 */ | |
2984 p = buf; | |
2985 s = buf; | |
2986 e = NULL; | |
2987 path_end = path; | |
2988 while (*path_end != NUL) | |
2989 { | |
2990 // May ignore a wildcard that has a backslash before it; it will | |
2991 // be removed by rem_backslash() or file_pat_to_reg_pat() below. | |
2992 if (path_end >= path + wildoff && rem_backslash(path_end)) | |
2993 *p++ = *path_end++; | |
2994 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/') | |
2995 { | |
2996 if (e != NULL) | |
2997 break; | |
2998 s = p + 1; | |
2999 } | |
3000 else if (path_end >= path + wildoff | |
3001 && vim_strchr((char_u *)"*?[~", *path_end) != NULL) | |
3002 e = p; | |
3003 if (has_mbyte) | |
3004 { | |
3005 len = (*mb_ptr2len)(path_end); | |
3006 STRNCPY(p, path_end, len); | |
3007 p += len; | |
3008 path_end += len; | |
3009 } | |
3010 else | |
3011 *p++ = *path_end++; | |
3012 } | |
3013 e = p; | |
3014 *e = NUL; | |
3015 | |
3016 // now we have one wildcard component between s and e | |
3017 // Remove backslashes between "wildoff" and the start of the wildcard | |
3018 // component. | |
3019 for (p = buf + wildoff; p < s; ++p) | |
3020 if (rem_backslash(p)) | |
3021 { | |
3022 STRMOVE(p, p + 1); | |
3023 --e; | |
3024 --s; | |
3025 } | |
3026 | |
3027 // Check for "**" between "s" and "e". | |
3028 for (p = s; p < e; ++p) | |
3029 if (p[0] == '*' && p[1] == '*') | |
3030 starstar = TRUE; | |
3031 | |
3032 starts_with_dot = *s == '.'; | |
3033 pat = file_pat_to_reg_pat(s, e, NULL, FALSE); | |
3034 if (pat == NULL) | |
3035 { | |
3036 vim_free(buf); | |
3037 return 0; | |
3038 } | |
3039 | |
3040 // compile the regexp into a program | |
3041 if (flags & (EW_NOERROR | EW_NOTWILD)) | |
3042 ++emsg_silent; | |
3043 regmatch.rm_ic = TRUE; // Always ignore case | |
3044 regmatch.regprog = vim_regcomp(pat, RE_MAGIC); | |
3045 if (flags & (EW_NOERROR | EW_NOTWILD)) | |
3046 --emsg_silent; | |
3047 vim_free(pat); | |
3048 | |
3049 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) | |
3050 { | |
3051 vim_free(buf); | |
3052 return 0; | |
3053 } | |
3054 | |
3055 // remember the pattern or file name being looked for | |
3056 matchname = vim_strsave(s); | |
3057 | |
3058 // If "**" is by itself, this is the first time we encounter it and more | |
3059 // is following then find matches without any directory. | |
3060 if (!didstar && stardepth < 100 && starstar && e - s == 2 | |
3061 && *path_end == '/') | |
3062 { | |
3063 STRCPY(s, path_end + 1); | |
3064 ++stardepth; | |
3065 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE); | |
3066 --stardepth; | |
3067 } | |
3068 | |
3069 // Scan all files in the directory with "dir/ *.*" | |
3070 STRCPY(s, "*.*"); | |
3071 wn = enc_to_utf16(buf, NULL); | |
3072 if (wn != NULL) | |
3073 hFind = FindFirstFileW(wn, &wfb); | |
3074 ok = (hFind != INVALID_HANDLE_VALUE); | |
3075 | |
3076 while (ok) | |
3077 { | |
3078 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here | |
3079 if (p == NULL) | |
3080 break; // out of memory | |
3081 | |
3082 // Ignore entries starting with a dot, unless when asked for. Accept | |
3083 // all entries found with "matchname". | |
3084 if ((p[0] != '.' || starts_with_dot | |
3085 || ((flags & EW_DODOT) | |
3086 && p[1] != NUL && (p[1] != '.' || p[2] != NUL))) | |
3087 && (matchname == NULL | |
3088 || (regmatch.regprog != NULL | |
3089 && vim_regexec(®match, p, (colnr_T)0)) | |
3090 || ((flags & EW_NOTWILD) | |
3091 && fnamencmp(path + (s - buf), p, e - s) == 0))) | |
3092 { | |
3093 STRCPY(s, p); | |
3094 len = (int)STRLEN(buf); | |
3095 | |
3096 if (starstar && stardepth < 100) | |
3097 { | |
3098 // For "**" in the pattern first go deeper in the tree to | |
3099 // find matches. | |
3100 STRCPY(buf + len, "/**"); | |
3101 STRCPY(buf + len + 3, path_end); | |
3102 ++stardepth; | |
3103 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE); | |
3104 --stardepth; | |
3105 } | |
3106 | |
3107 STRCPY(buf + len, path_end); | |
3108 if (mch_has_exp_wildcard(path_end)) | |
3109 { | |
3110 // need to expand another component of the path | |
3111 // remove backslashes for the remaining components only | |
3112 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE); | |
3113 } | |
3114 else | |
3115 { | |
3116 // no more wildcards, check if there is a match | |
3117 // remove backslashes for the remaining components only | |
3118 if (*path_end != 0) | |
3119 backslash_halve(buf + len + 1); | |
3120 if (mch_getperm(buf) >= 0) // add existing file | |
3121 addfile(gap, buf, flags); | |
3122 } | |
3123 } | |
3124 | |
3125 vim_free(p); | |
3126 ok = FindNextFileW(hFind, &wfb); | |
3127 | |
3128 // If no more matches and no match was used, try expanding the name | |
3129 // itself. Finds the long name of a short filename. | |
3130 if (!ok && matchname != NULL && gap->ga_len == start_len) | |
3131 { | |
3132 STRCPY(s, matchname); | |
3133 FindClose(hFind); | |
3134 vim_free(wn); | |
3135 wn = enc_to_utf16(buf, NULL); | |
3136 if (wn != NULL) | |
3137 hFind = FindFirstFileW(wn, &wfb); | |
3138 else | |
3139 hFind = INVALID_HANDLE_VALUE; | |
3140 ok = (hFind != INVALID_HANDLE_VALUE); | |
3141 VIM_CLEAR(matchname); | |
3142 } | |
3143 } | |
3144 | |
3145 FindClose(hFind); | |
3146 vim_free(wn); | |
3147 vim_free(buf); | |
3148 vim_regfree(regmatch.regprog); | |
3149 vim_free(matchname); | |
3150 | |
3151 matches = gap->ga_len - start_len; | |
3152 if (matches > 0) | |
3153 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches, | |
3154 sizeof(char_u *), pstrcmp); | |
3155 return matches; | |
3156 } | |
3157 | |
3158 int | |
3159 mch_expandpath( | |
3160 garray_T *gap, | |
3161 char_u *path, | |
3162 int flags) // EW_* flags | |
3163 { | |
3164 return dos_expandpath(gap, path, 0, flags, FALSE); | |
3165 } | |
3166 #endif // MSWIN | |
3167 | |
3168 #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \ | |
3169 || defined(PROTO) | |
3170 /* | |
3171 * Unix style wildcard expansion code. | |
3172 * It's here because it's used both for Unix and Mac. | |
3173 */ | |
3174 static int | |
3175 pstrcmp(const void *a, const void *b) | |
3176 { | |
3177 return (pathcmp(*(char **)a, *(char **)b, -1)); | |
3178 } | |
3179 | |
3180 /* | |
3181 * Recursively expand one path component into all matching files and/or | |
3182 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc. | |
3183 * "path" has backslashes before chars that are not to be expanded, starting | |
3184 * at "path + wildoff". | |
3185 * Return the number of matches found. | |
3186 * NOTE: much of this is identical to dos_expandpath(), keep in sync! | |
3187 */ | |
3188 int | |
3189 unix_expandpath( | |
3190 garray_T *gap, | |
3191 char_u *path, | |
3192 int wildoff, | |
3193 int flags, // EW_* flags | |
3194 int didstar) // expanded "**" once already | |
3195 { | |
3196 char_u *buf; | |
3197 char_u *path_end; | |
3198 char_u *p, *s, *e; | |
3199 int start_len = gap->ga_len; | |
3200 char_u *pat; | |
3201 regmatch_T regmatch; | |
3202 int starts_with_dot; | |
3203 int matches; | |
3204 int len; | |
3205 int starstar = FALSE; | |
3206 static int stardepth = 0; // depth for "**" expansion | |
3207 | |
3208 DIR *dirp; | |
3209 struct dirent *dp; | |
3210 | |
3211 // Expanding "**" may take a long time, check for CTRL-C. | |
3212 if (stardepth > 0) | |
3213 { | |
3214 ui_breakcheck(); | |
3215 if (got_int) | |
3216 return 0; | |
3217 } | |
3218 | |
3219 // make room for file name | |
3220 buf = alloc(STRLEN(path) + BASENAMELEN + 5); | |
3221 if (buf == NULL) | |
3222 return 0; | |
3223 | |
3224 /* | |
3225 * Find the first part in the path name that contains a wildcard. | |
3226 * When EW_ICASE is set every letter is considered to be a wildcard. | |
3227 * Copy it into "buf", including the preceding characters. | |
3228 */ | |
3229 p = buf; | |
3230 s = buf; | |
3231 e = NULL; | |
3232 path_end = path; | |
3233 while (*path_end != NUL) | |
3234 { | |
3235 // May ignore a wildcard that has a backslash before it; it will | |
3236 // be removed by rem_backslash() or file_pat_to_reg_pat() below. | |
3237 if (path_end >= path + wildoff && rem_backslash(path_end)) | |
3238 *p++ = *path_end++; | |
3239 else if (*path_end == '/') | |
3240 { | |
3241 if (e != NULL) | |
3242 break; | |
3243 s = p + 1; | |
3244 } | |
3245 else if (path_end >= path + wildoff | |
3246 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL | |
3247 || (!p_fic && (flags & EW_ICASE) | |
3248 && isalpha(PTR2CHAR(path_end))))) | |
3249 e = p; | |
3250 if (has_mbyte) | |
3251 { | |
3252 len = (*mb_ptr2len)(path_end); | |
3253 STRNCPY(p, path_end, len); | |
3254 p += len; | |
3255 path_end += len; | |
3256 } | |
3257 else | |
3258 *p++ = *path_end++; | |
3259 } | |
3260 e = p; | |
3261 *e = NUL; | |
3262 | |
3263 // Now we have one wildcard component between "s" and "e". | |
3264 // Remove backslashes between "wildoff" and the start of the wildcard | |
3265 // component. | |
3266 for (p = buf + wildoff; p < s; ++p) | |
3267 if (rem_backslash(p)) | |
3268 { | |
3269 STRMOVE(p, p + 1); | |
3270 --e; | |
3271 --s; | |
3272 } | |
3273 | |
3274 // Check for "**" between "s" and "e". | |
3275 for (p = s; p < e; ++p) | |
3276 if (p[0] == '*' && p[1] == '*') | |
3277 starstar = TRUE; | |
3278 | |
3279 // convert the file pattern to a regexp pattern | |
3280 starts_with_dot = *s == '.'; | |
3281 pat = file_pat_to_reg_pat(s, e, NULL, FALSE); | |
3282 if (pat == NULL) | |
3283 { | |
3284 vim_free(buf); | |
3285 return 0; | |
3286 } | |
3287 | |
3288 // compile the regexp into a program | |
3289 if (flags & EW_ICASE) | |
3290 regmatch.rm_ic = TRUE; // 'wildignorecase' set | |
3291 else | |
3292 regmatch.rm_ic = p_fic; // ignore case when 'fileignorecase' is set | |
3293 if (flags & (EW_NOERROR | EW_NOTWILD)) | |
3294 ++emsg_silent; | |
3295 regmatch.regprog = vim_regcomp(pat, RE_MAGIC); | |
3296 if (flags & (EW_NOERROR | EW_NOTWILD)) | |
3297 --emsg_silent; | |
3298 vim_free(pat); | |
3299 | |
3300 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0) | |
3301 { | |
3302 vim_free(buf); | |
3303 return 0; | |
3304 } | |
3305 | |
3306 // If "**" is by itself, this is the first time we encounter it and more | |
3307 // is following then find matches without any directory. | |
3308 if (!didstar && stardepth < 100 && starstar && e - s == 2 | |
3309 && *path_end == '/') | |
3310 { | |
3311 STRCPY(s, path_end + 1); | |
3312 ++stardepth; | |
3313 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE); | |
3314 --stardepth; | |
3315 } | |
3316 | |
3317 // open the directory for scanning | |
3318 *s = NUL; | |
3319 dirp = opendir(*buf == NUL ? "." : (char *)buf); | |
3320 | |
3321 // Find all matching entries | |
3322 if (dirp != NULL) | |
3323 { | |
3324 for (;;) | |
3325 { | |
3326 dp = readdir(dirp); | |
3327 if (dp == NULL) | |
3328 break; | |
3329 if ((dp->d_name[0] != '.' || starts_with_dot | |
3330 || ((flags & EW_DODOT) | |
3331 && dp->d_name[1] != NUL | |
3332 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL))) | |
3333 && ((regmatch.regprog != NULL && vim_regexec(®match, | |
3334 (char_u *)dp->d_name, (colnr_T)0)) | |
3335 || ((flags & EW_NOTWILD) | |
3336 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0))) | |
3337 { | |
3338 STRCPY(s, dp->d_name); | |
3339 len = STRLEN(buf); | |
3340 | |
3341 if (starstar && stardepth < 100) | |
3342 { | |
3343 // For "**" in the pattern first go deeper in the tree to | |
3344 // find matches. | |
3345 STRCPY(buf + len, "/**"); | |
3346 STRCPY(buf + len + 3, path_end); | |
3347 ++stardepth; | |
3348 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE); | |
3349 --stardepth; | |
3350 } | |
3351 | |
3352 STRCPY(buf + len, path_end); | |
3353 if (mch_has_exp_wildcard(path_end)) // handle more wildcards | |
3354 { | |
3355 // need to expand another component of the path | |
3356 // remove backslashes for the remaining components only | |
3357 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE); | |
3358 } | |
3359 else | |
3360 { | |
3361 stat_T sb; | |
3362 | |
3363 // no more wildcards, check if there is a match | |
3364 // remove backslashes for the remaining components only | |
3365 if (*path_end != NUL) | |
3366 backslash_halve(buf + len + 1); | |
3367 // add existing file or symbolic link | |
3368 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0 | |
3369 : mch_getperm(buf) >= 0) | |
3370 { | |
3371 #ifdef MACOS_CONVERT | |
3372 size_t precomp_len = STRLEN(buf)+1; | |
3373 char_u *precomp_buf = | |
3374 mac_precompose_path(buf, precomp_len, &precomp_len); | |
3375 | |
3376 if (precomp_buf) | |
3377 { | |
3378 mch_memmove(buf, precomp_buf, precomp_len); | |
3379 vim_free(precomp_buf); | |
3380 } | |
3381 #endif | |
3382 addfile(gap, buf, flags); | |
3383 } | |
3384 } | |
3385 } | |
3386 } | |
3387 | |
3388 closedir(dirp); | |
3389 } | |
3390 | |
3391 vim_free(buf); | |
3392 vim_regfree(regmatch.regprog); | |
3393 | |
3394 matches = gap->ga_len - start_len; | |
3395 if (matches > 0) | |
3396 qsort(((char_u **)gap->ga_data) + start_len, matches, | |
3397 sizeof(char_u *), pstrcmp); | |
3398 return matches; | |
3399 } | |
3400 #endif | |
3401 | |
3402 /* | |
3403 * Return TRUE if "p" contains what looks like an environment variable. | |
3404 * Allowing for escaping. | |
3405 */ | |
3406 static int | |
3407 has_env_var(char_u *p) | |
3408 { | |
3409 for ( ; *p; MB_PTR_ADV(p)) | |
3410 { | |
3411 if (*p == '\\' && p[1] != NUL) | |
3412 ++p; | |
3413 else if (vim_strchr((char_u *) | |
3414 #if defined(MSWIN) | |
3415 "$%" | |
3416 #else | |
3417 "$" | |
3418 #endif | |
3419 , *p) != NULL) | |
3420 return TRUE; | |
3421 } | |
3422 return FALSE; | |
3423 } | |
3424 | |
3425 #ifdef SPECIAL_WILDCHAR | |
3426 /* | |
3427 * Return TRUE if "p" contains a special wildcard character, one that Vim | |
3428 * cannot expand, requires using a shell. | |
3429 */ | |
3430 static int | |
3431 has_special_wildchar(char_u *p) | |
3432 { | |
3433 for ( ; *p; MB_PTR_ADV(p)) | |
3434 { | |
3435 // Disallow line break characters. | |
3436 if (*p == '\r' || *p == '\n') | |
3437 break; | |
3438 // Allow for escaping. | |
3439 if (*p == '\\' && p[1] != NUL && p[1] != '\r' && p[1] != '\n') | |
3440 ++p; | |
3441 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL) | |
3442 { | |
3443 // A { must be followed by a matching }. | |
3444 if (*p == '{' && vim_strchr(p, '}') == NULL) | |
3445 continue; | |
3446 // A quote and backtick must be followed by another one. | |
3447 if ((*p == '`' || *p == '\'') && vim_strchr(p, *p) == NULL) | |
3448 continue; | |
3449 return TRUE; | |
3450 } | |
3451 } | |
3452 return FALSE; | |
3453 } | |
3454 #endif | |
3455 | |
3456 /* | |
3457 * Generic wildcard expansion code. | |
3458 * | |
3459 * Characters in "pat" that should not be expanded must be preceded with a | |
3460 * backslash. E.g., "/path\ with\ spaces/my\*star*" | |
3461 * | |
3462 * Return FAIL when no single file was found. In this case "num_file" is not | |
3463 * set, and "file" may contain an error message. | |
3464 * Return OK when some files found. "num_file" is set to the number of | |
3465 * matches, "file" to the array of matches. Call FreeWild() later. | |
3466 */ | |
3467 int | |
3468 gen_expand_wildcards( | |
3469 int num_pat, // number of input patterns | |
3470 char_u **pat, // array of input patterns | |
3471 int *num_file, // resulting number of files | |
3472 char_u ***file, // array of resulting files | |
3473 int flags) // EW_* flags | |
3474 { | |
3475 int i; | |
3476 garray_T ga; | |
3477 char_u *p; | |
3478 static int recursive = FALSE; | |
3479 int add_pat; | |
3480 int retval = OK; | |
3481 #if defined(FEAT_SEARCHPATH) | |
3482 int did_expand_in_path = FALSE; | |
3483 #endif | |
3484 | |
3485 /* | |
3486 * expand_env() is called to expand things like "~user". If this fails, | |
3487 * it calls ExpandOne(), which brings us back here. In this case, always | |
3488 * call the machine specific expansion function, if possible. Otherwise, | |
3489 * return FAIL. | |
3490 */ | |
3491 if (recursive) | |
3492 #ifdef SPECIAL_WILDCHAR | |
3493 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); | |
3494 #else | |
3495 return FAIL; | |
3496 #endif | |
3497 | |
3498 #ifdef SPECIAL_WILDCHAR | |
3499 /* | |
3500 * If there are any special wildcard characters which we cannot handle | |
3501 * here, call machine specific function for all the expansion. This | |
3502 * avoids starting the shell for each argument separately. | |
3503 * For `=expr` do use the internal function. | |
3504 */ | |
3505 for (i = 0; i < num_pat; i++) | |
3506 { | |
3507 if (has_special_wildchar(pat[i]) | |
3508 # ifdef VIM_BACKTICK | |
3509 && !(vim_backtick(pat[i]) && pat[i][1] == '=') | |
3510 # endif | |
3511 ) | |
3512 return mch_expand_wildcards(num_pat, pat, num_file, file, flags); | |
3513 } | |
3514 #endif | |
3515 | |
3516 recursive = TRUE; | |
3517 | |
3518 /* | |
3519 * The matching file names are stored in a growarray. Init it empty. | |
3520 */ | |
3521 ga_init2(&ga, (int)sizeof(char_u *), 30); | |
3522 | |
3523 for (i = 0; i < num_pat; ++i) | |
3524 { | |
3525 add_pat = -1; | |
3526 p = pat[i]; | |
3527 | |
3528 #ifdef VIM_BACKTICK | |
3529 if (vim_backtick(p)) | |
3530 { | |
3531 add_pat = expand_backtick(&ga, p, flags); | |
3532 if (add_pat == -1) | |
3533 retval = FAIL; | |
3534 } | |
3535 else | |
3536 #endif | |
3537 { | |
3538 /* | |
3539 * First expand environment variables, "~/" and "~user/". | |
3540 */ | |
3541 if ((has_env_var(p) && !(flags & EW_NOTENV)) || *p == '~') | |
3542 { | |
3543 p = expand_env_save_opt(p, TRUE); | |
3544 if (p == NULL) | |
3545 p = pat[i]; | |
3546 #ifdef UNIX | |
3547 /* | |
3548 * On Unix, if expand_env() can't expand an environment | |
3549 * variable, use the shell to do that. Discard previously | |
3550 * found file names and start all over again. | |
3551 */ | |
3552 else if (has_env_var(p) || *p == '~') | |
3553 { | |
3554 vim_free(p); | |
3555 ga_clear_strings(&ga); | |
3556 i = mch_expand_wildcards(num_pat, pat, num_file, file, | |
3557 flags|EW_KEEPDOLLAR); | |
3558 recursive = FALSE; | |
3559 return i; | |
3560 } | |
3561 #endif | |
3562 } | |
3563 | |
3564 /* | |
3565 * If there are wildcards: Expand file names and add each match to | |
3566 * the list. If there is no match, and EW_NOTFOUND is given, add | |
3567 * the pattern. | |
3568 * If there are no wildcards: Add the file name if it exists or | |
3569 * when EW_NOTFOUND is given. | |
3570 */ | |
3571 if (mch_has_exp_wildcard(p)) | |
3572 { | |
3573 #if defined(FEAT_SEARCHPATH) | |
3574 if ((flags & EW_PATH) | |
3575 && !mch_isFullName(p) | |
3576 && !(p[0] == '.' | |
3577 && (vim_ispathsep(p[1]) | |
3578 || (p[1] == '.' && vim_ispathsep(p[2])))) | |
3579 ) | |
3580 { | |
3581 // :find completion where 'path' is used. | |
3582 // Recursiveness is OK here. | |
3583 recursive = FALSE; | |
3584 add_pat = expand_in_path(&ga, p, flags); | |
3585 recursive = TRUE; | |
3586 did_expand_in_path = TRUE; | |
3587 } | |
3588 else | |
3589 #endif | |
3590 add_pat = mch_expandpath(&ga, p, flags); | |
3591 } | |
3592 } | |
3593 | |
3594 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND))) | |
3595 { | |
3596 char_u *t = backslash_halve_save(p); | |
3597 | |
3598 // When EW_NOTFOUND is used, always add files and dirs. Makes | |
3599 // "vim c:/" work. | |
3600 if (flags & EW_NOTFOUND) | |
3601 addfile(&ga, t, flags | EW_DIR | EW_FILE); | |
3602 else | |
3603 addfile(&ga, t, flags); | |
3604 | |
3605 if (t != p) | |
3606 vim_free(t); | |
3607 } | |
3608 | |
3609 #if defined(FEAT_SEARCHPATH) | |
3610 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH)) | |
3611 uniquefy_paths(&ga, p); | |
3612 #endif | |
3613 if (p != pat[i]) | |
3614 vim_free(p); | |
3615 } | |
3616 | |
3617 *num_file = ga.ga_len; | |
3618 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)""; | |
3619 | |
3620 recursive = FALSE; | |
3621 | |
3622 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL; | |
3623 } | |
3624 | |
3625 /* | |
3626 * Add a file to a file list. Accepted flags: | |
3627 * EW_DIR add directories | |
3628 * EW_FILE add files | |
3629 * EW_EXEC add executable files | |
3630 * EW_NOTFOUND add even when it doesn't exist | |
3631 * EW_ADDSLASH add slash after directory name | |
3632 * EW_ALLLINKS add symlink also when the referred file does not exist | |
3633 */ | |
3634 void | |
3635 addfile( | |
3636 garray_T *gap, | |
3637 char_u *f, /* filename */ | |
3638 int flags) | |
3639 { | |
3640 char_u *p; | |
3641 int isdir; | |
3642 stat_T sb; | |
3643 | |
3644 // if the file/dir/link doesn't exist, may not add it | |
3645 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS) | |
3646 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0)) | |
3647 return; | |
3648 | |
3649 #ifdef FNAME_ILLEGAL | |
3650 // if the file/dir contains illegal characters, don't add it | |
3651 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL) | |
3652 return; | |
3653 #endif | |
3654 | |
3655 isdir = mch_isdir(f); | |
3656 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE))) | |
3657 return; | |
3658 | |
3659 // If the file isn't executable, may not add it. Do accept directories. | |
3660 // When invoked from expand_shellcmd() do not use $PATH. | |
3661 if (!isdir && (flags & EW_EXEC) | |
3662 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD))) | |
3663 return; | |
3664 | |
3665 // Make room for another item in the file list. | |
3666 if (ga_grow(gap, 1) == FAIL) | |
3667 return; | |
3668 | |
3669 p = alloc(STRLEN(f) + 1 + isdir); | |
3670 if (p == NULL) | |
3671 return; | |
3672 | |
3673 STRCPY(p, f); | |
3674 #ifdef BACKSLASH_IN_FILENAME | |
3675 slash_adjust(p); | |
3676 #endif | |
3677 /* | |
3678 * Append a slash or backslash after directory names if none is present. | |
3679 */ | |
3680 #ifndef DONT_ADD_PATHSEP_TO_DIR | |
3681 if (isdir && (flags & EW_ADDSLASH)) | |
3682 add_pathsep(p); | |
3683 #endif | |
3684 ((char_u **)gap->ga_data)[gap->ga_len++] = p; | |
3685 } | |
3686 | |
3687 /* | |
3688 * Free the list of files returned by expand_wildcards() or other expansion | |
3689 * functions. | |
3690 */ | |
3691 void | |
3692 FreeWild(int count, char_u **files) | |
3693 { | |
3694 if (count <= 0 || files == NULL) | |
3695 return; | |
3696 while (count--) | |
3697 vim_free(files[count]); | |
3698 vim_free(files); | |
3699 } | |
3700 | |
3701 /* | |
3702 * Compare path "p[]" to "q[]". | |
3703 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]" | |
3704 * Return value like strcmp(p, q), but consider path separators. | |
3705 */ | |
3706 int | |
3707 pathcmp(const char *p, const char *q, int maxlen) | |
3708 { | |
3709 int i, j; | |
3710 int c1, c2; | |
3711 const char *s = NULL; | |
3712 | |
3713 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);) | |
3714 { | |
3715 c1 = PTR2CHAR((char_u *)p + i); | |
3716 c2 = PTR2CHAR((char_u *)q + j); | |
3717 | |
3718 // End of "p": check if "q" also ends or just has a slash. | |
3719 if (c1 == NUL) | |
3720 { | |
3721 if (c2 == NUL) // full match | |
3722 return 0; | |
3723 s = q; | |
3724 i = j; | |
3725 break; | |
3726 } | |
3727 | |
3728 // End of "q": check if "p" just has a slash. | |
3729 if (c2 == NUL) | |
3730 { | |
3731 s = p; | |
3732 break; | |
3733 } | |
3734 | |
3735 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2) | |
3736 #ifdef BACKSLASH_IN_FILENAME | |
3737 // consider '/' and '\\' to be equal | |
3738 && !((c1 == '/' && c2 == '\\') | |
3739 || (c1 == '\\' && c2 == '/')) | |
3740 #endif | |
3741 ) | |
3742 { | |
3743 if (vim_ispathsep(c1)) | |
3744 return -1; | |
3745 if (vim_ispathsep(c2)) | |
3746 return 1; | |
3747 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2) | |
3748 : c1 - c2; // no match | |
3749 } | |
3750 | |
3751 i += MB_PTR2LEN((char_u *)p + i); | |
3752 j += MB_PTR2LEN((char_u *)q + j); | |
3753 } | |
3754 if (s == NULL) // "i" or "j" ran into "maxlen" | |
3755 return 0; | |
3756 | |
3757 c1 = PTR2CHAR((char_u *)s + i); | |
3758 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i)); | |
3759 // ignore a trailing slash, but not "//" or ":/" | |
3760 if (c2 == NUL | |
3761 && i > 0 | |
3762 && !after_pathsep((char_u *)s, (char_u *)s + i) | |
3763 #ifdef BACKSLASH_IN_FILENAME | |
3764 && (c1 == '/' || c1 == '\\') | |
3765 #else | |
3766 && c1 == '/' | |
3767 #endif | |
3768 ) | |
3769 return 0; // match with trailing slash | |
3770 if (s == q) | |
3771 return -1; // no match | |
3772 return 1; | |
3773 } | |
3774 | |
3775 /* | |
3776 * Return TRUE if "name" is a full (absolute) path name or URL. | |
3777 */ | |
3778 int | |
3779 vim_isAbsName(char_u *name) | |
3780 { | |
3781 return (path_with_url(name) != 0 || mch_isFullName(name)); | |
3782 } | |
3783 | |
3784 /* | |
3785 * Get absolute file name into buffer "buf[len]". | |
3786 * | |
3787 * return FAIL for failure, OK otherwise | |
3788 */ | |
3789 int | |
3790 vim_FullName( | |
3791 char_u *fname, | |
3792 char_u *buf, | |
3793 int len, | |
3794 int force) // force expansion even when already absolute | |
3795 { | |
3796 int retval = OK; | |
3797 int url; | |
3798 | |
3799 *buf = NUL; | |
3800 if (fname == NULL) | |
3801 return FAIL; | |
3802 | |
3803 url = path_with_url(fname); | |
3804 if (!url) | |
3805 retval = mch_FullName(fname, buf, len, force); | |
3806 if (url || retval == FAIL) | |
3807 { | |
3808 // something failed; use the file name (truncate when too long) | |
3809 vim_strncpy(buf, fname, len - 1); | |
3810 } | |
3811 #if defined(MSWIN) | |
3812 slash_adjust(buf); | |
3813 #endif | |
3814 return retval; | |
3815 } |