blob: 230f4870d45bcd5e2b3ff3ebb868b8037035e77f [file] [log] [blame]
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * findfile.c: Search for files in directories listed in 'path'
12 */
13
14#include "vim.h"
15
16/*
17 * File searching functions for 'path', 'tags' and 'cdpath' options.
18 * External visible functions:
19 * vim_findfile_init() creates/initialises the search context
20 * vim_findfile_free_visited() free list of visited files/dirs of search
21 * context
22 * vim_findfile() find a file in the search context
23 * vim_findfile_cleanup() cleanup/free search context created by
24 * vim_findfile_init()
25 *
26 * All static functions and variables start with 'ff_'
27 *
28 * In general it works like this:
29 * First you create yourself a search context by calling vim_findfile_init().
30 * It is possible to give a search context from a previous call to
31 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
32 * until you are satisfied with the result or it returns NULL. On every call it
33 * returns the next file which matches the conditions given to
34 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
35 *
36 * It is possible to call vim_findfile_init() again to reinitialise your search
37 * with some new parameters. Don't forget to pass your old search context to
38 * it, so it can reuse it and especially reuse the list of already visited
39 * directories. If you want to delete the list of already visited directories
40 * simply call vim_findfile_free_visited().
41 *
42 * When you are done call vim_findfile_cleanup() to free the search context.
43 *
44 * The function vim_findfile_init() has a long comment, which describes the
45 * needed parameters.
46 *
47 *
48 *
49 * ATTENTION:
50 * ==========
51 * Also we use an allocated search context here, this functions are NOT
52 * thread-safe!!!!!
53 *
54 * To minimize parameter passing (or because I'm to lazy), only the
55 * external visible functions get a search context as a parameter. This is
56 * then assigned to a static global, which is used throughout the local
57 * functions.
58 */
59
60/*
61 * type for the directory search stack
62 */
63typedef struct ff_stack
64{
65 struct ff_stack *ffs_prev;
66
67 // the fix part (no wildcards) and the part containing the wildcards
68 // of the search path
69 char_u *ffs_fix_path;
70#ifdef FEAT_PATH_EXTRA
71 char_u *ffs_wc_path;
72#endif
73
74 // files/dirs found in the above directory, matched by the first wildcard
75 // of wc_part
76 char_u **ffs_filearray;
77 int ffs_filearray_size;
78 char_u ffs_filearray_cur; // needed for partly handled dirs
79
80 // to store status of partly handled directories
81 // 0: we work on this directory for the first time
82 // 1: this directory was partly searched in an earlier step
83 int ffs_stage;
84
85 // How deep are we in the directory tree?
86 // Counts backward from value of level parameter to vim_findfile_init
87 int ffs_level;
88
89 // Did we already expand '**' to an empty string?
90 int ffs_star_star_empty;
91} ff_stack_T;
92
93/*
94 * type for already visited directories or files.
95 */
96typedef struct ff_visited
97{
98 struct ff_visited *ffv_next;
99
100#ifdef FEAT_PATH_EXTRA
101 // Visited directories are different if the wildcard string are
102 // different. So we have to save it.
103 char_u *ffv_wc_path;
104#endif
105 // for unix use inode etc for comparison (needed because of links), else
106 // use filename.
107#ifdef UNIX
108 int ffv_dev_valid; // ffv_dev and ffv_ino were set
109 dev_t ffv_dev; // device number
110 ino_t ffv_ino; // inode number
111#endif
112 // The memory for this struct is allocated according to the length of
113 // ffv_fname.
114 char_u ffv_fname[1]; // actually longer
115} ff_visited_T;
116
117/*
118 * We might have to manage several visited lists during a search.
119 * This is especially needed for the tags option. If tags is set to:
120 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
121 * So we have to do 3 searches:
122 * 1) search from the current files directory downward for the file "tags"
123 * 2) search from the current files directory downward for the file "TAGS"
124 * 3) search from Vims current directory downwards for the file "tags"
125 * As you can see, the first and the third search are for the same file, so for
126 * the third search we can use the visited list of the first search. For the
127 * second search we must start from a empty visited list.
128 * The struct ff_visited_list_hdr is used to manage a linked list of already
129 * visited lists.
130 */
131typedef struct ff_visited_list_hdr
132{
133 struct ff_visited_list_hdr *ffvl_next;
134
135 // the filename the attached visited list is for
136 char_u *ffvl_filename;
137
138 ff_visited_T *ffvl_visited_list;
139
140} ff_visited_list_hdr_T;
141
142
143/*
144 * '**' can be expanded to several directory levels.
145 * Set the default maximum depth.
146 */
147#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
148
149/*
150 * The search context:
151 * ffsc_stack_ptr: the stack for the dirs to search
152 * ffsc_visited_list: the currently active visited list
153 * ffsc_dir_visited_list: the currently active visited list for search dirs
154 * ffsc_visited_lists_list: the list of all visited lists
155 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
156 * ffsc_file_to_search: the file to search for
157 * ffsc_start_dir: the starting directory, if search path was relative
158 * ffsc_fix_path: the fix part of the given path (without wildcards)
159 * Needed for upward search.
160 * ffsc_wc_path: the part of the given path containing wildcards
161 * ffsc_level: how many levels of dirs to search downwards
162 * ffsc_stopdirs_v: array of stop directories for upward search
163 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
164 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
165 */
166typedef struct ff_search_ctx_T
167{
168 ff_stack_T *ffsc_stack_ptr;
169 ff_visited_list_hdr_T *ffsc_visited_list;
170 ff_visited_list_hdr_T *ffsc_dir_visited_list;
171 ff_visited_list_hdr_T *ffsc_visited_lists_list;
172 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
173 char_u *ffsc_file_to_search;
174 char_u *ffsc_start_dir;
175 char_u *ffsc_fix_path;
176#ifdef FEAT_PATH_EXTRA
177 char_u *ffsc_wc_path;
178 int ffsc_level;
179 char_u **ffsc_stopdirs_v;
180#endif
181 int ffsc_find_what;
182 int ffsc_tagfile;
183} ff_search_ctx_T;
184
185// locally needed functions
186#ifdef FEAT_PATH_EXTRA
187static int ff_check_visited(ff_visited_T **, char_u *, char_u *);
188#else
189static int ff_check_visited(ff_visited_T **, char_u *);
190#endif
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200191static void vim_findfile_free_visited(void *search_ctx_arg);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100192static void vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp);
193static void ff_free_visited_list(ff_visited_T *vl);
194static ff_visited_list_hdr_T* ff_get_visited_list(char_u *, ff_visited_list_hdr_T **list_headp);
195
196static void ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr);
197static ff_stack_T *ff_pop(ff_search_ctx_T *search_ctx);
198static void ff_clear(ff_search_ctx_T *search_ctx);
199static void ff_free_stack_element(ff_stack_T *stack_ptr);
200#ifdef FEAT_PATH_EXTRA
201static ff_stack_T *ff_create_stack_element(char_u *, char_u *, int, int);
202#else
203static ff_stack_T *ff_create_stack_element(char_u *, int, int);
204#endif
205#ifdef FEAT_PATH_EXTRA
206static int ff_path_in_stoplist(char_u *, int, char_u **);
207#endif
208
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100209static char_u *ff_expand_buffer = NULL; // used for expanding filenames
210
211#if 0
212/*
213 * if someone likes findfirst/findnext, here are the functions
214 * NOT TESTED!!
215 */
216
217static void *ff_fn_search_context = NULL;
218
219 char_u *
220vim_findfirst(char_u *path, char_u *filename, int level)
221{
222 ff_fn_search_context =
223 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
224 ff_fn_search_context, rel_fname);
225 if (NULL == ff_fn_search_context)
226 return NULL;
227 else
228 return vim_findnext()
229}
230
231 char_u *
232vim_findnext(void)
233{
234 char_u *ret = vim_findfile(ff_fn_search_context);
235
236 if (NULL == ret)
237 {
238 vim_findfile_cleanup(ff_fn_search_context);
239 ff_fn_search_context = NULL;
240 }
241 return ret;
242}
243#endif
244
245/*
246 * Initialization routine for vim_findfile().
247 *
248 * Returns the newly allocated search context or NULL if an error occurred.
249 *
250 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
251 * with the search context.
252 *
253 * Find the file 'filename' in the directory 'path'.
254 * The parameter 'path' may contain wildcards. If so only search 'level'
255 * directories deep. The parameter 'level' is the absolute maximum and is
256 * not related to restricts given to the '**' wildcard. If 'level' is 100
257 * and you use '**200' vim_findfile() will stop after 100 levels.
258 *
259 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
260 * escape special characters.
261 *
262 * If 'stopdirs' is not NULL and nothing is found downward, the search is
263 * restarted on the next higher directory level. This is repeated until the
264 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
265 * format ";*<dirname>*\(;<dirname>\)*;\=$".
266 *
267 * If the 'path' is relative, the starting dir for the search is either VIM's
268 * current dir or if the path starts with "./" the current files dir.
269 * If the 'path' is absolute, the starting dir is that part of the path before
270 * the first wildcard.
271 *
272 * Upward search is only done on the starting dir.
273 *
274 * If 'free_visited' is TRUE the list of already visited files/directories is
275 * cleared. Set this to FALSE if you just want to search from another
276 * directory, but want to be sure that no directory from a previous search is
277 * searched again. This is useful if you search for a file at different places.
278 * The list of visited files/dirs can also be cleared with the function
279 * vim_findfile_free_visited().
280 *
281 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
282 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
283 *
284 * A search context returned by a previous call to vim_findfile_init() can be
285 * passed in the parameter "search_ctx_arg". This context is reused and
286 * reinitialized with the new parameters. The list of already visited
287 * directories from this context is only deleted if the parameter
288 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
289 * if the reinitialization fails.
290 *
291 * If you don't have a search context from a previous call "search_ctx_arg"
292 * must be NULL.
293 *
294 * This function silently ignores a few errors, vim_findfile() will have
295 * limited functionality then.
296 */
297 void *
298vim_findfile_init(
299 char_u *path,
300 char_u *filename,
301 char_u *stopdirs UNUSED,
302 int level,
303 int free_visited,
304 int find_what,
305 void *search_ctx_arg,
306 int tagfile, // expanding names of tags files
307 char_u *rel_fname) // file name to use for "."
308{
309#ifdef FEAT_PATH_EXTRA
310 char_u *wc_part;
311#endif
312 ff_stack_T *sptr;
313 ff_search_ctx_T *search_ctx;
314
315 // If a search context is given by the caller, reuse it, else allocate a
316 // new one.
317 if (search_ctx_arg != NULL)
318 search_ctx = search_ctx_arg;
319 else
320 {
Bram Moolenaara80faa82020-04-12 19:37:17 +0200321 search_ctx = ALLOC_CLEAR_ONE(ff_search_ctx_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100322 if (search_ctx == NULL)
323 goto error_return;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100324 }
325 search_ctx->ffsc_find_what = find_what;
326 search_ctx->ffsc_tagfile = tagfile;
327
328 // clear the search context, but NOT the visited lists
329 ff_clear(search_ctx);
330
331 // clear visited list if wanted
332 if (free_visited == TRUE)
333 vim_findfile_free_visited(search_ctx);
334 else
335 {
336 // Reuse old visited lists. Get the visited list for the given
337 // filename. If no list for the current filename exists, creates a new
338 // one.
339 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
340 &search_ctx->ffsc_visited_lists_list);
341 if (search_ctx->ffsc_visited_list == NULL)
342 goto error_return;
343 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
344 &search_ctx->ffsc_dir_visited_lists_list);
345 if (search_ctx->ffsc_dir_visited_list == NULL)
346 goto error_return;
347 }
348
349 if (ff_expand_buffer == NULL)
350 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200351 ff_expand_buffer = alloc(MAXPATHL);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100352 if (ff_expand_buffer == NULL)
353 goto error_return;
354 }
355
356 // Store information on starting dir now if path is relative.
357 // If path is absolute, we do that later.
358 if (path[0] == '.'
359 && (vim_ispathsep(path[1]) || path[1] == NUL)
360 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
361 && rel_fname != NULL)
362 {
363 int len = (int)(gettail(rel_fname) - rel_fname);
364
365 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
366 {
367 // Make the start dir an absolute path name.
368 vim_strncpy(ff_expand_buffer, rel_fname, len);
369 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
370 }
371 else
372 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
373 if (search_ctx->ffsc_start_dir == NULL)
374 goto error_return;
375 if (*++path != NUL)
376 ++path;
377 }
378 else if (*path == NUL || !vim_isAbsName(path))
379 {
380#ifdef BACKSLASH_IN_FILENAME
381 // "c:dir" needs "c:" to be expanded, otherwise use current dir
382 if (*path != NUL && path[1] == ':')
383 {
384 char_u drive[3];
385
386 drive[0] = path[0];
387 drive[1] = ':';
388 drive[2] = NUL;
389 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
390 goto error_return;
391 path += 2;
392 }
393 else
394#endif
395 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
396 goto error_return;
397
398 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
399 if (search_ctx->ffsc_start_dir == NULL)
400 goto error_return;
401
402#ifdef BACKSLASH_IN_FILENAME
403 // A path that starts with "/dir" is relative to the drive, not to the
404 // directory (but not for "//machine/dir"). Only use the drive name.
405 if ((*path == '/' || *path == '\\')
406 && path[1] != path[0]
407 && search_ctx->ffsc_start_dir[1] == ':')
408 search_ctx->ffsc_start_dir[2] = NUL;
409#endif
410 }
411
412#ifdef FEAT_PATH_EXTRA
413 /*
414 * If stopdirs are given, split them into an array of pointers.
415 * If this fails (mem allocation), there is no upward search at all or a
416 * stop directory is not recognized -> continue silently.
417 * If stopdirs just contains a ";" or is empty,
418 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
419 * is handled as unlimited upward search. See function
420 * ff_path_in_stoplist() for details.
421 */
422 if (stopdirs != NULL)
423 {
424 char_u *walker = stopdirs;
425 int dircount;
426
427 while (*walker == ';')
428 walker++;
429
430 dircount = 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200431 search_ctx->ffsc_stopdirs_v = ALLOC_ONE(char_u *);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100432
433 if (search_ctx->ffsc_stopdirs_v != NULL)
434 {
435 do
436 {
437 char_u *helper;
438 void *ptr;
439
440 helper = walker;
441 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
442 (dircount + 1) * sizeof(char_u *));
443 if (ptr)
444 search_ctx->ffsc_stopdirs_v = ptr;
445 else
446 // ignore, keep what we have and continue
447 break;
448 walker = vim_strchr(walker, ';');
449 if (walker)
450 {
451 search_ctx->ffsc_stopdirs_v[dircount-1] =
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200452 vim_strnsave(helper, walker - helper);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100453 walker++;
454 }
455 else
456 // this might be "", which means ascent till top
457 // of directory tree.
458 search_ctx->ffsc_stopdirs_v[dircount-1] =
459 vim_strsave(helper);
460
461 dircount++;
462
463 } while (walker != NULL);
464 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
465 }
466 }
467#endif
468
469#ifdef FEAT_PATH_EXTRA
470 search_ctx->ffsc_level = level;
471
472 /*
473 * split into:
474 * -fix path
475 * -wildcard_stuff (might be NULL)
476 */
477 wc_part = vim_strchr(path, '*');
478 if (wc_part != NULL)
479 {
480 int llevel;
481 int len;
482 char *errpt;
483
484 // save the fix part of the path
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200485 search_ctx->ffsc_fix_path = vim_strnsave(path, wc_part - path);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100486
487 /*
488 * copy wc_path and add restricts to the '**' wildcard.
489 * The octet after a '**' is used as a (binary) counter.
490 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
491 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
492 * For EBCDIC you get different character values.
493 * If no restrict is given after '**' the default is used.
494 * Due to this technique the path looks awful if you print it as a
495 * string.
496 */
497 len = 0;
498 while (*wc_part != NUL)
499 {
500 if (len + 5 >= MAXPATHL)
501 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000502 emsg(_(e_path_too_long_for_completion));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100503 break;
504 }
505 if (STRNCMP(wc_part, "**", 2) == 0)
506 {
507 ff_expand_buffer[len++] = *wc_part++;
508 ff_expand_buffer[len++] = *wc_part++;
509
510 llevel = strtol((char *)wc_part, &errpt, 10);
511 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
512 ff_expand_buffer[len++] = llevel;
513 else if ((char_u *)errpt != wc_part && llevel == 0)
514 // restrict is 0 -> remove already added '**'
515 len -= 2;
516 else
517 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
518 wc_part = (char_u *)errpt;
519 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
520 {
Bram Moolenaareaaac012022-01-02 17:00:40 +0000521 semsg(_(e_invalid_path_number_must_be_at_end_of_path_or_be_followed_by_str), PATHSEPSTR);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100522 goto error_return;
523 }
524 }
525 else
526 ff_expand_buffer[len++] = *wc_part++;
527 }
528 ff_expand_buffer[len] = NUL;
529 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
530
531 if (search_ctx->ffsc_wc_path == NULL)
532 goto error_return;
533 }
534 else
535#endif
536 search_ctx->ffsc_fix_path = vim_strsave(path);
537
538 if (search_ctx->ffsc_start_dir == NULL)
539 {
540 // store the fix part as startdir.
541 // This is needed if the parameter path is fully qualified.
542 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
543 if (search_ctx->ffsc_start_dir == NULL)
544 goto error_return;
545 search_ctx->ffsc_fix_path[0] = NUL;
546 }
547
548 // create an absolute path
549 if (STRLEN(search_ctx->ffsc_start_dir)
550 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
551 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000552 emsg(_(e_path_too_long_for_completion));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100553 goto error_return;
554 }
555 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
556 add_pathsep(ff_expand_buffer);
557 {
558 int eb_len = (int)STRLEN(ff_expand_buffer);
559 char_u *buf = alloc(eb_len
560 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
561
562 STRCPY(buf, ff_expand_buffer);
563 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
564 if (mch_isdir(buf))
565 {
566 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
567 add_pathsep(ff_expand_buffer);
568 }
569#ifdef FEAT_PATH_EXTRA
570 else
571 {
572 char_u *p = gettail(search_ctx->ffsc_fix_path);
573 char_u *wc_path = NULL;
574 char_u *temp = NULL;
575 int len = 0;
576
577 if (p > search_ctx->ffsc_fix_path)
578 {
Christian Brabandt7a4ca322021-07-25 15:08:05 +0200579 // do not add '..' to the path and start upwards searching
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100580 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Christian Brabandt7a4ca322021-07-25 15:08:05 +0200581 if ((len >= 2
582 && STRNCMP(search_ctx->ffsc_fix_path, "..", 2) == 0)
583 && (len == 2
584 || search_ctx->ffsc_fix_path[2] == PATHSEP))
585 {
586 vim_free(buf);
587 goto error_return;
588 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100589 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
590 add_pathsep(ff_expand_buffer);
591 }
592 else
593 len = (int)STRLEN(search_ctx->ffsc_fix_path);
594
595 if (search_ctx->ffsc_wc_path != NULL)
596 {
597 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar51e14382019-05-25 20:21:28 +0200598 temp = alloc(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100599 + STRLEN(search_ctx->ffsc_fix_path + len)
Bram Moolenaar51e14382019-05-25 20:21:28 +0200600 + 1);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100601 if (temp == NULL || wc_path == NULL)
602 {
603 vim_free(buf);
604 vim_free(temp);
605 vim_free(wc_path);
606 goto error_return;
607 }
608
609 STRCPY(temp, search_ctx->ffsc_fix_path + len);
610 STRCAT(temp, search_ctx->ffsc_wc_path);
611 vim_free(search_ctx->ffsc_wc_path);
612 vim_free(wc_path);
613 search_ctx->ffsc_wc_path = temp;
614 }
615 }
616#endif
617 vim_free(buf);
618 }
619
620 sptr = ff_create_stack_element(ff_expand_buffer,
621#ifdef FEAT_PATH_EXTRA
622 search_ctx->ffsc_wc_path,
623#endif
624 level, 0);
625
626 if (sptr == NULL)
627 goto error_return;
628
629 ff_push(search_ctx, sptr);
630
631 search_ctx->ffsc_file_to_search = vim_strsave(filename);
632 if (search_ctx->ffsc_file_to_search == NULL)
633 goto error_return;
634
635 return search_ctx;
636
637error_return:
638 /*
639 * We clear the search context now!
640 * Even when the caller gave us a (perhaps valid) context we free it here,
641 * as we might have already destroyed it.
642 */
643 vim_findfile_cleanup(search_ctx);
644 return NULL;
645}
646
647#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
648/*
649 * Get the stopdir string. Check that ';' is not escaped.
650 */
651 char_u *
652vim_findfile_stopdir(char_u *buf)
653{
654 char_u *r_ptr = buf;
655
656 while (*r_ptr != NUL && *r_ptr != ';')
657 {
658 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
659 {
660 // Overwrite the escape char,
661 // use STRLEN(r_ptr) to move the trailing '\0'.
662 STRMOVE(r_ptr, r_ptr + 1);
663 r_ptr++;
664 }
665 r_ptr++;
666 }
667 if (*r_ptr == ';')
668 {
669 *r_ptr = 0;
670 r_ptr++;
671 }
672 else if (*r_ptr == NUL)
673 r_ptr = NULL;
674 return r_ptr;
675}
676#endif
677
678/*
679 * Clean up the given search context. Can handle a NULL pointer.
680 */
681 void
682vim_findfile_cleanup(void *ctx)
683{
684 if (ctx == NULL)
685 return;
686
687 vim_findfile_free_visited(ctx);
688 ff_clear(ctx);
689 vim_free(ctx);
690}
691
692/*
693 * Find a file in a search context.
694 * The search context was created with vim_findfile_init() above.
695 * Return a pointer to an allocated file name or NULL if nothing found.
696 * To get all matching files call this function until you get NULL.
697 *
698 * If the passed search_context is NULL, NULL is returned.
699 *
700 * The search algorithm is depth first. To change this replace the
701 * stack with a list (don't forget to leave partly searched directories on the
702 * top of the list).
703 */
704 char_u *
705vim_findfile(void *search_ctx_arg)
706{
707 char_u *file_path;
708#ifdef FEAT_PATH_EXTRA
709 char_u *rest_of_wildcards;
710 char_u *path_end = NULL;
711#endif
712 ff_stack_T *stackp;
713#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
714 int len;
715#endif
716 int i;
717 char_u *p;
718#ifdef FEAT_SEARCHPATH
719 char_u *suf;
720#endif
721 ff_search_ctx_T *search_ctx;
722
723 if (search_ctx_arg == NULL)
724 return NULL;
725
726 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
727
728 /*
729 * filepath is used as buffer for various actions and as the storage to
730 * return a found filename.
731 */
Bram Moolenaar51e14382019-05-25 20:21:28 +0200732 if ((file_path = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100733 return NULL;
734
735#ifdef FEAT_PATH_EXTRA
736 // store the end of the start dir -- needed for upward search
737 if (search_ctx->ffsc_start_dir != NULL)
738 path_end = &search_ctx->ffsc_start_dir[
739 STRLEN(search_ctx->ffsc_start_dir)];
740#endif
741
742#ifdef FEAT_PATH_EXTRA
743 // upward search loop
744 for (;;)
745 {
746#endif
747 // downward search loop
748 for (;;)
749 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000750 // check if user wants to stop the search
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100751 ui_breakcheck();
752 if (got_int)
753 break;
754
755 // get directory to work on from stack
756 stackp = ff_pop(search_ctx);
757 if (stackp == NULL)
758 break;
759
760 /*
761 * TODO: decide if we leave this test in
762 *
763 * GOOD: don't search a directory(-tree) twice.
764 * BAD: - check linked list for every new directory entered.
765 * - check for double files also done below
766 *
767 * Here we check if we already searched this directory.
768 * We already searched a directory if:
769 * 1) The directory is the same.
770 * 2) We would use the same wildcard string.
771 *
772 * Good if you have links on same directory via several ways
773 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
774 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
775 *
776 * This check is only needed for directories we work on for the
777 * first time (hence stackp->ff_filearray == NULL)
778 */
779 if (stackp->ffs_filearray == NULL
780 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
781 ->ffvl_visited_list,
782 stackp->ffs_fix_path
783#ifdef FEAT_PATH_EXTRA
784 , stackp->ffs_wc_path
785#endif
786 ) == FAIL)
787 {
788#ifdef FF_VERBOSE
789 if (p_verbose >= 5)
790 {
791 verbose_enter_scroll();
792 smsg("Already Searched: %s (%s)",
793 stackp->ffs_fix_path, stackp->ffs_wc_path);
794 // don't overwrite this either
795 msg_puts("\n");
796 verbose_leave_scroll();
797 }
798#endif
799 ff_free_stack_element(stackp);
800 continue;
801 }
802#ifdef FF_VERBOSE
803 else if (p_verbose >= 5)
804 {
805 verbose_enter_scroll();
806 smsg("Searching: %s (%s)",
807 stackp->ffs_fix_path, stackp->ffs_wc_path);
808 // don't overwrite this either
809 msg_puts("\n");
810 verbose_leave_scroll();
811 }
812#endif
813
814 // check depth
815 if (stackp->ffs_level <= 0)
816 {
817 ff_free_stack_element(stackp);
818 continue;
819 }
820
821 file_path[0] = NUL;
822
823 /*
824 * If no filearray till now expand wildcards
825 * The function expand_wildcards() can handle an array of paths
826 * and all possible expands are returned in one array. We use this
827 * to handle the expansion of '**' into an empty string.
828 */
829 if (stackp->ffs_filearray == NULL)
830 {
831 char_u *dirptrs[2];
832
833 // we use filepath to build the path expand_wildcards() should
834 // expand.
835 dirptrs[0] = file_path;
836 dirptrs[1] = NULL;
837
838 // if we have a start dir copy it in
839 if (!vim_isAbsName(stackp->ffs_fix_path)
840 && search_ctx->ffsc_start_dir)
841 {
842 if (STRLEN(search_ctx->ffsc_start_dir) + 1 < MAXPATHL)
843 {
844 STRCPY(file_path, search_ctx->ffsc_start_dir);
845 add_pathsep(file_path);
846 }
847 else
848 {
849 ff_free_stack_element(stackp);
850 goto fail;
851 }
852 }
853
854 // append the fix part of the search path
855 if (STRLEN(file_path) + STRLEN(stackp->ffs_fix_path) + 1
856 < MAXPATHL)
857 {
858 STRCAT(file_path, stackp->ffs_fix_path);
859 add_pathsep(file_path);
860 }
861 else
862 {
863 ff_free_stack_element(stackp);
864 goto fail;
865 }
866
867#ifdef FEAT_PATH_EXTRA
868 rest_of_wildcards = stackp->ffs_wc_path;
869 if (*rest_of_wildcards != NUL)
870 {
871 len = (int)STRLEN(file_path);
872 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
873 {
874 // pointer to the restrict byte
875 // The restrict byte is not a character!
876 p = rest_of_wildcards + 2;
877
878 if (*p > 0)
879 {
880 (*p)--;
881 if (len + 1 < MAXPATHL)
882 file_path[len++] = '*';
883 else
884 {
885 ff_free_stack_element(stackp);
886 goto fail;
887 }
888 }
889
890 if (*p == 0)
891 {
892 // remove '**<numb> from wildcards
893 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
894 }
895 else
896 rest_of_wildcards += 3;
897
898 if (stackp->ffs_star_star_empty == 0)
899 {
900 // if not done before, expand '**' to empty
901 stackp->ffs_star_star_empty = 1;
902 dirptrs[1] = stackp->ffs_fix_path;
903 }
904 }
905
906 /*
907 * Here we copy until the next path separator or the end of
908 * the path. If we stop at a path separator, there is
909 * still something else left. This is handled below by
910 * pushing every directory returned from expand_wildcards()
911 * on the stack again for further search.
912 */
913 while (*rest_of_wildcards
914 && !vim_ispathsep(*rest_of_wildcards))
915 if (len + 1 < MAXPATHL)
916 file_path[len++] = *rest_of_wildcards++;
917 else
918 {
919 ff_free_stack_element(stackp);
920 goto fail;
921 }
922
923 file_path[len] = NUL;
924 if (vim_ispathsep(*rest_of_wildcards))
925 rest_of_wildcards++;
926 }
927#endif
928
929 /*
930 * Expand wildcards like "*" and "$VAR".
931 * If the path is a URL don't try this.
932 */
933 if (path_with_url(dirptrs[0]))
934 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200935 stackp->ffs_filearray = ALLOC_ONE(char_u *);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100936 if (stackp->ffs_filearray != NULL
937 && (stackp->ffs_filearray[0]
938 = vim_strsave(dirptrs[0])) != NULL)
939 stackp->ffs_filearray_size = 1;
940 else
941 stackp->ffs_filearray_size = 0;
942 }
943 else
944 // Add EW_NOTWILD because the expanded path may contain
945 // wildcard characters that are to be taken literally.
946 // This is a bit of a hack.
947 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
948 &stackp->ffs_filearray_size,
949 &stackp->ffs_filearray,
950 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
951
952 stackp->ffs_filearray_cur = 0;
953 stackp->ffs_stage = 0;
954 }
955#ifdef FEAT_PATH_EXTRA
956 else
957 rest_of_wildcards = &stackp->ffs_wc_path[
958 STRLEN(stackp->ffs_wc_path)];
959#endif
960
961 if (stackp->ffs_stage == 0)
962 {
963 // this is the first time we work on this directory
964#ifdef FEAT_PATH_EXTRA
965 if (*rest_of_wildcards == NUL)
966#endif
967 {
968 /*
969 * We don't have further wildcards to expand, so we have to
970 * check for the final file now.
971 */
972 for (i = stackp->ffs_filearray_cur;
973 i < stackp->ffs_filearray_size; ++i)
974 {
975 if (!path_with_url(stackp->ffs_filearray[i])
976 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100977 continue; // not a directory
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100978
979 // prepare the filename to be checked for existence
980 // below
981 if (STRLEN(stackp->ffs_filearray[i]) + 1
982 + STRLEN(search_ctx->ffsc_file_to_search)
983 < MAXPATHL)
984 {
985 STRCPY(file_path, stackp->ffs_filearray[i]);
986 add_pathsep(file_path);
987 STRCAT(file_path, search_ctx->ffsc_file_to_search);
988 }
989 else
990 {
991 ff_free_stack_element(stackp);
992 goto fail;
993 }
994
995 /*
996 * Try without extra suffix and then with suffixes
997 * from 'suffixesadd'.
998 */
999#ifdef FEAT_SEARCHPATH
1000 len = (int)STRLEN(file_path);
1001 if (search_ctx->ffsc_tagfile)
1002 suf = (char_u *)"";
1003 else
1004 suf = curbuf->b_p_sua;
1005 for (;;)
1006#endif
1007 {
1008 // if file exists and we didn't already find it
1009 if ((path_with_url(file_path)
1010 || (mch_getperm(file_path) >= 0
1011 && (search_ctx->ffsc_find_what
1012 == FINDFILE_BOTH
1013 || ((search_ctx->ffsc_find_what
1014 == FINDFILE_DIR)
1015 == mch_isdir(file_path)))))
1016#ifndef FF_VERBOSE
1017 && (ff_check_visited(
1018 &search_ctx->ffsc_visited_list->ffvl_visited_list,
1019 file_path
1020#ifdef FEAT_PATH_EXTRA
1021 , (char_u *)""
1022#endif
1023 ) == OK)
1024#endif
1025 )
1026 {
1027#ifdef FF_VERBOSE
1028 if (ff_check_visited(
1029 &search_ctx->ffsc_visited_list->ffvl_visited_list,
1030 file_path
1031#ifdef FEAT_PATH_EXTRA
1032 , (char_u *)""
1033#endif
1034 ) == FAIL)
1035 {
1036 if (p_verbose >= 5)
1037 {
1038 verbose_enter_scroll();
1039 smsg("Already: %s",
1040 file_path);
1041 // don't overwrite this either
1042 msg_puts("\n");
1043 verbose_leave_scroll();
1044 }
1045 continue;
1046 }
1047#endif
1048
1049 // push dir to examine rest of subdirs later
1050 stackp->ffs_filearray_cur = i + 1;
1051 ff_push(search_ctx, stackp);
1052
1053 if (!path_with_url(file_path))
1054 simplify_filename(file_path);
1055 if (mch_dirname(ff_expand_buffer, MAXPATHL)
1056 == OK)
1057 {
1058 p = shorten_fname(file_path,
1059 ff_expand_buffer);
1060 if (p != NULL)
1061 STRMOVE(file_path, p);
1062 }
1063#ifdef FF_VERBOSE
1064 if (p_verbose >= 5)
1065 {
1066 verbose_enter_scroll();
1067 smsg("HIT: %s", file_path);
1068 // don't overwrite this either
1069 msg_puts("\n");
1070 verbose_leave_scroll();
1071 }
1072#endif
1073 return file_path;
1074 }
1075
1076#ifdef FEAT_SEARCHPATH
1077 // Not found or found already, try next suffix.
1078 if (*suf == NUL)
1079 break;
1080 copy_option_part(&suf, file_path + len,
1081 MAXPATHL - len, ",");
1082#endif
1083 }
1084 }
1085 }
1086#ifdef FEAT_PATH_EXTRA
1087 else
1088 {
1089 /*
1090 * still wildcards left, push the directories for further
1091 * search
1092 */
1093 for (i = stackp->ffs_filearray_cur;
1094 i < stackp->ffs_filearray_size; ++i)
1095 {
1096 if (!mch_isdir(stackp->ffs_filearray[i]))
1097 continue; // not a directory
1098
1099 ff_push(search_ctx,
1100 ff_create_stack_element(
1101 stackp->ffs_filearray[i],
1102 rest_of_wildcards,
1103 stackp->ffs_level - 1, 0));
1104 }
1105 }
1106#endif
1107 stackp->ffs_filearray_cur = 0;
1108 stackp->ffs_stage = 1;
1109 }
1110
1111#ifdef FEAT_PATH_EXTRA
1112 /*
1113 * if wildcards contains '**' we have to descent till we reach the
1114 * leaves of the directory tree.
1115 */
1116 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
1117 {
1118 for (i = stackp->ffs_filearray_cur;
1119 i < stackp->ffs_filearray_size; ++i)
1120 {
1121 if (fnamecmp(stackp->ffs_filearray[i],
1122 stackp->ffs_fix_path) == 0)
1123 continue; // don't repush same directory
1124 if (!mch_isdir(stackp->ffs_filearray[i]))
1125 continue; // not a directory
1126 ff_push(search_ctx,
1127 ff_create_stack_element(stackp->ffs_filearray[i],
1128 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
1129 }
1130 }
1131#endif
1132
1133 // we are done with the current directory
1134 ff_free_stack_element(stackp);
1135
1136 }
1137
1138#ifdef FEAT_PATH_EXTRA
1139 // If we reached this, we didn't find anything downwards.
1140 // Let's check if we should do an upward search.
1141 if (search_ctx->ffsc_start_dir
1142 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
1143 {
1144 ff_stack_T *sptr;
1145
1146 // is the last starting directory in the stop list?
1147 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
1148 (int)(path_end - search_ctx->ffsc_start_dir),
1149 search_ctx->ffsc_stopdirs_v) == TRUE)
1150 break;
1151
1152 // cut of last dir
1153 while (path_end > search_ctx->ffsc_start_dir
1154 && vim_ispathsep(*path_end))
1155 path_end--;
1156 while (path_end > search_ctx->ffsc_start_dir
1157 && !vim_ispathsep(path_end[-1]))
1158 path_end--;
1159 *path_end = 0;
1160 path_end--;
1161
1162 if (*search_ctx->ffsc_start_dir == 0)
1163 break;
1164
1165 if (STRLEN(search_ctx->ffsc_start_dir) + 1
1166 + STRLEN(search_ctx->ffsc_fix_path) < MAXPATHL)
1167 {
1168 STRCPY(file_path, search_ctx->ffsc_start_dir);
1169 add_pathsep(file_path);
1170 STRCAT(file_path, search_ctx->ffsc_fix_path);
1171 }
1172 else
1173 goto fail;
1174
1175 // create a new stack entry
1176 sptr = ff_create_stack_element(file_path,
1177 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
1178 if (sptr == NULL)
1179 break;
1180 ff_push(search_ctx, sptr);
1181 }
1182 else
1183 break;
1184 }
1185#endif
1186
1187fail:
1188 vim_free(file_path);
1189 return NULL;
1190}
1191
1192/*
1193 * Free the list of lists of visited files and directories
1194 * Can handle it if the passed search_context is NULL;
1195 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001196 static void
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001197vim_findfile_free_visited(void *search_ctx_arg)
1198{
1199 ff_search_ctx_T *search_ctx;
1200
1201 if (search_ctx_arg == NULL)
1202 return;
1203
1204 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
1205 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
1206 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
1207}
1208
1209 static void
1210vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp)
1211{
1212 ff_visited_list_hdr_T *vp;
1213
1214 while (*list_headp != NULL)
1215 {
1216 vp = (*list_headp)->ffvl_next;
1217 ff_free_visited_list((*list_headp)->ffvl_visited_list);
1218
1219 vim_free((*list_headp)->ffvl_filename);
1220 vim_free(*list_headp);
1221 *list_headp = vp;
1222 }
1223 *list_headp = NULL;
1224}
1225
1226 static void
1227ff_free_visited_list(ff_visited_T *vl)
1228{
1229 ff_visited_T *vp;
1230
1231 while (vl != NULL)
1232 {
1233 vp = vl->ffv_next;
1234#ifdef FEAT_PATH_EXTRA
1235 vim_free(vl->ffv_wc_path);
1236#endif
1237 vim_free(vl);
1238 vl = vp;
1239 }
1240 vl = NULL;
1241}
1242
1243/*
1244 * Returns the already visited list for the given filename. If none is found it
1245 * allocates a new one.
1246 */
1247 static ff_visited_list_hdr_T*
1248ff_get_visited_list(
1249 char_u *filename,
1250 ff_visited_list_hdr_T **list_headp)
1251{
1252 ff_visited_list_hdr_T *retptr = NULL;
1253
1254 // check if a visited list for the given filename exists
1255 if (*list_headp != NULL)
1256 {
1257 retptr = *list_headp;
1258 while (retptr != NULL)
1259 {
1260 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
1261 {
1262#ifdef FF_VERBOSE
1263 if (p_verbose >= 5)
1264 {
1265 verbose_enter_scroll();
1266 smsg("ff_get_visited_list: FOUND list for %s",
1267 filename);
1268 // don't overwrite this either
1269 msg_puts("\n");
1270 verbose_leave_scroll();
1271 }
1272#endif
1273 return retptr;
1274 }
1275 retptr = retptr->ffvl_next;
1276 }
1277 }
1278
1279#ifdef FF_VERBOSE
1280 if (p_verbose >= 5)
1281 {
1282 verbose_enter_scroll();
1283 smsg("ff_get_visited_list: new list for %s", filename);
1284 // don't overwrite this either
1285 msg_puts("\n");
1286 verbose_leave_scroll();
1287 }
1288#endif
1289
1290 /*
1291 * if we reach this we didn't find a list and we have to allocate new list
1292 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001293 retptr = ALLOC_ONE(ff_visited_list_hdr_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001294 if (retptr == NULL)
1295 return NULL;
1296
1297 retptr->ffvl_visited_list = NULL;
1298 retptr->ffvl_filename = vim_strsave(filename);
1299 if (retptr->ffvl_filename == NULL)
1300 {
1301 vim_free(retptr);
1302 return NULL;
1303 }
1304 retptr->ffvl_next = *list_headp;
1305 *list_headp = retptr;
1306
1307 return retptr;
1308}
1309
1310#ifdef FEAT_PATH_EXTRA
1311/*
1312 * check if two wildcard paths are equal. Returns TRUE or FALSE.
1313 * They are equal if:
1314 * - both paths are NULL
1315 * - they have the same length
1316 * - char by char comparison is OK
1317 * - the only differences are in the counters behind a '**', so
1318 * '**\20' is equal to '**\24'
1319 */
1320 static int
1321ff_wc_equal(char_u *s1, char_u *s2)
1322{
1323 int i, j;
1324 int c1 = NUL;
1325 int c2 = NUL;
1326 int prev1 = NUL;
1327 int prev2 = NUL;
1328
1329 if (s1 == s2)
1330 return TRUE;
1331
1332 if (s1 == NULL || s2 == NULL)
1333 return FALSE;
1334
1335 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
1336 {
1337 c1 = PTR2CHAR(s1 + i);
1338 c2 = PTR2CHAR(s2 + j);
1339
1340 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
1341 && (prev1 != '*' || prev2 != '*'))
1342 return FALSE;
1343 prev2 = prev1;
1344 prev1 = c1;
1345
Bram Moolenaar1614a142019-10-06 22:00:13 +02001346 i += mb_ptr2len(s1 + i);
1347 j += mb_ptr2len(s2 + j);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001348 }
1349 return s1[i] == s2[j];
1350}
1351#endif
1352
1353/*
1354 * maintains the list of already visited files and dirs
1355 * returns FAIL if the given file/dir is already in the list
1356 * returns OK if it is newly added
1357 *
1358 * TODO: What to do on memory allocation problems?
1359 * -> return TRUE - Better the file is found several times instead of
1360 * never.
1361 */
1362 static int
1363ff_check_visited(
1364 ff_visited_T **visited_list,
1365 char_u *fname
1366#ifdef FEAT_PATH_EXTRA
1367 , char_u *wc_path
1368#endif
1369 )
1370{
1371 ff_visited_T *vp;
1372#ifdef UNIX
1373 stat_T st;
1374 int url = FALSE;
1375#endif
1376
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001377 // For a URL we only compare the name, otherwise we compare the
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001378 // device/inode (unix) or the full path name (not Unix).
1379 if (path_with_url(fname))
1380 {
1381 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
1382#ifdef UNIX
1383 url = TRUE;
1384#endif
1385 }
1386 else
1387 {
1388 ff_expand_buffer[0] = NUL;
1389#ifdef UNIX
1390 if (mch_stat((char *)fname, &st) < 0)
1391#else
1392 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
1393#endif
1394 return FAIL;
1395 }
1396
1397 // check against list of already visited files
1398 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
1399 {
1400 if (
1401#ifdef UNIX
1402 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
1403 && vp->ffv_ino == st.st_ino)
1404 :
1405#endif
1406 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
1407 )
1408 {
1409#ifdef FEAT_PATH_EXTRA
1410 // are the wildcard parts equal
1411 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
1412#endif
1413 // already visited
1414 return FAIL;
1415 }
1416 }
1417
1418 /*
1419 * New file/dir. Add it to the list of visited files/dirs.
1420 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001421 vp = alloc(sizeof(ff_visited_T) + STRLEN(ff_expand_buffer));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001422
1423 if (vp != NULL)
1424 {
1425#ifdef UNIX
1426 if (!url)
1427 {
1428 vp->ffv_dev_valid = TRUE;
1429 vp->ffv_ino = st.st_ino;
1430 vp->ffv_dev = st.st_dev;
1431 vp->ffv_fname[0] = NUL;
1432 }
1433 else
1434 {
1435 vp->ffv_dev_valid = FALSE;
1436#endif
1437 STRCPY(vp->ffv_fname, ff_expand_buffer);
1438#ifdef UNIX
1439 }
1440#endif
1441#ifdef FEAT_PATH_EXTRA
1442 if (wc_path != NULL)
1443 vp->ffv_wc_path = vim_strsave(wc_path);
1444 else
1445 vp->ffv_wc_path = NULL;
1446#endif
1447
1448 vp->ffv_next = *visited_list;
1449 *visited_list = vp;
1450 }
1451
1452 return OK;
1453}
1454
1455/*
1456 * create stack element from given path pieces
1457 */
1458 static ff_stack_T *
1459ff_create_stack_element(
1460 char_u *fix_part,
1461#ifdef FEAT_PATH_EXTRA
1462 char_u *wc_part,
1463#endif
1464 int level,
1465 int star_star_empty)
1466{
1467 ff_stack_T *new;
1468
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001469 new = ALLOC_ONE(ff_stack_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001470 if (new == NULL)
1471 return NULL;
1472
1473 new->ffs_prev = NULL;
1474 new->ffs_filearray = NULL;
1475 new->ffs_filearray_size = 0;
1476 new->ffs_filearray_cur = 0;
1477 new->ffs_stage = 0;
1478 new->ffs_level = level;
1479 new->ffs_star_star_empty = star_star_empty;
1480
1481 // the following saves NULL pointer checks in vim_findfile
1482 if (fix_part == NULL)
1483 fix_part = (char_u *)"";
1484 new->ffs_fix_path = vim_strsave(fix_part);
1485
1486#ifdef FEAT_PATH_EXTRA
1487 if (wc_part == NULL)
1488 wc_part = (char_u *)"";
1489 new->ffs_wc_path = vim_strsave(wc_part);
1490#endif
1491
1492 if (new->ffs_fix_path == NULL
1493#ifdef FEAT_PATH_EXTRA
1494 || new->ffs_wc_path == NULL
1495#endif
1496 )
1497 {
1498 ff_free_stack_element(new);
1499 new = NULL;
1500 }
1501
1502 return new;
1503}
1504
1505/*
1506 * Push a dir on the directory stack.
1507 */
1508 static void
1509ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)
1510{
1511 // check for NULL pointer, not to return an error to the user, but
1512 // to prevent a crash
1513 if (stack_ptr != NULL)
1514 {
1515 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
1516 search_ctx->ffsc_stack_ptr = stack_ptr;
1517 }
1518}
1519
1520/*
1521 * Pop a dir from the directory stack.
1522 * Returns NULL if stack is empty.
1523 */
1524 static ff_stack_T *
1525ff_pop(ff_search_ctx_T *search_ctx)
1526{
1527 ff_stack_T *sptr;
1528
1529 sptr = search_ctx->ffsc_stack_ptr;
1530 if (search_ctx->ffsc_stack_ptr != NULL)
1531 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
1532
1533 return sptr;
1534}
1535
1536/*
1537 * free the given stack element
1538 */
1539 static void
1540ff_free_stack_element(ff_stack_T *stack_ptr)
1541{
1542 // vim_free handles possible NULL pointers
1543 vim_free(stack_ptr->ffs_fix_path);
1544#ifdef FEAT_PATH_EXTRA
1545 vim_free(stack_ptr->ffs_wc_path);
1546#endif
1547
1548 if (stack_ptr->ffs_filearray != NULL)
1549 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
1550
1551 vim_free(stack_ptr);
1552}
1553
1554/*
1555 * Clear the search context, but NOT the visited list.
1556 */
1557 static void
1558ff_clear(ff_search_ctx_T *search_ctx)
1559{
1560 ff_stack_T *sptr;
1561
1562 // clear up stack
1563 while ((sptr = ff_pop(search_ctx)) != NULL)
1564 ff_free_stack_element(sptr);
1565
1566 vim_free(search_ctx->ffsc_file_to_search);
1567 vim_free(search_ctx->ffsc_start_dir);
1568 vim_free(search_ctx->ffsc_fix_path);
1569#ifdef FEAT_PATH_EXTRA
1570 vim_free(search_ctx->ffsc_wc_path);
1571#endif
1572
1573#ifdef FEAT_PATH_EXTRA
1574 if (search_ctx->ffsc_stopdirs_v != NULL)
1575 {
1576 int i = 0;
1577
1578 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
1579 {
1580 vim_free(search_ctx->ffsc_stopdirs_v[i]);
1581 i++;
1582 }
1583 vim_free(search_ctx->ffsc_stopdirs_v);
1584 }
1585 search_ctx->ffsc_stopdirs_v = NULL;
1586#endif
1587
1588 // reset everything
1589 search_ctx->ffsc_file_to_search = NULL;
1590 search_ctx->ffsc_start_dir = NULL;
1591 search_ctx->ffsc_fix_path = NULL;
1592#ifdef FEAT_PATH_EXTRA
1593 search_ctx->ffsc_wc_path = NULL;
1594 search_ctx->ffsc_level = 0;
1595#endif
1596}
1597
1598#ifdef FEAT_PATH_EXTRA
1599/*
1600 * check if the given path is in the stopdirs
1601 * returns TRUE if yes else FALSE
1602 */
1603 static int
1604ff_path_in_stoplist(char_u *path, int path_len, char_u **stopdirs_v)
1605{
1606 int i = 0;
1607
1608 // eat up trailing path separators, except the first
1609 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
1610 path_len--;
1611
1612 // if no path consider it as match
1613 if (path_len == 0)
1614 return TRUE;
1615
1616 for (i = 0; stopdirs_v[i] != NULL; i++)
1617 {
1618 if ((int)STRLEN(stopdirs_v[i]) > path_len)
1619 {
1620 // match for parent directory. So '/home' also matches
1621 // '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
1622 // '/home/r' would also match '/home/rks'
1623 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
1624 && vim_ispathsep(stopdirs_v[i][path_len]))
1625 return TRUE;
1626 }
1627 else
1628 {
1629 if (fnamecmp(stopdirs_v[i], path) == 0)
1630 return TRUE;
1631 }
1632 }
1633 return FALSE;
1634}
1635#endif
1636
1637#if defined(FEAT_SEARCHPATH) || defined(PROTO)
1638/*
1639 * Find the file name "ptr[len]" in the path. Also finds directory names.
1640 *
1641 * On the first call set the parameter 'first' to TRUE to initialize
1642 * the search. For repeating calls to FALSE.
1643 *
1644 * Repeating calls will return other files called 'ptr[len]' from the path.
1645 *
1646 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
1647 * don't need valid values.
1648 *
1649 * If nothing found on the first call the option FNAME_MESS will issue the
1650 * message:
1651 * 'Can't find file "<file>" in path'
1652 * On repeating calls:
1653 * 'No more file "<file>" found in path'
1654 *
1655 * options:
1656 * FNAME_MESS give error message when not found
1657 *
1658 * Uses NameBuff[]!
1659 *
1660 * Returns an allocated string for the file name. NULL for error.
1661 *
1662 */
1663 char_u *
1664find_file_in_path(
1665 char_u *ptr, // file name
1666 int len, // length of file name
1667 int options,
1668 int first, // use count'th matching file name
1669 char_u *rel_fname) // file name searching relative to
1670{
1671 return find_file_in_path_option(ptr, len, options, first,
1672 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
1673 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
1674}
1675
1676static char_u *ff_file_to_find = NULL;
1677static void *fdip_search_ctx = NULL;
1678
1679# if defined(EXITFREE) || defined(PROTO)
1680 void
1681free_findfile(void)
1682{
1683 vim_free(ff_file_to_find);
1684 vim_findfile_cleanup(fdip_search_ctx);
1685 vim_free(ff_expand_buffer);
1686}
1687# endif
1688
1689/*
1690 * Find the directory name "ptr[len]" in the path.
1691 *
1692 * options:
1693 * FNAME_MESS give error message when not found
1694 * FNAME_UNESC unescape backslashes.
1695 *
1696 * Uses NameBuff[]!
1697 *
1698 * Returns an allocated string for the file name. NULL for error.
1699 */
1700 char_u *
1701find_directory_in_path(
1702 char_u *ptr, // file name
1703 int len, // length of file name
1704 int options,
1705 char_u *rel_fname) // file name searching relative to
1706{
1707 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
1708 FINDFILE_DIR, rel_fname, (char_u *)"");
1709}
1710
1711 char_u *
1712find_file_in_path_option(
1713 char_u *ptr, // file name
1714 int len, // length of file name
1715 int options,
1716 int first, // use count'th matching file name
1717 char_u *path_option, // p_path or p_cdpath
1718 int find_what, // FINDFILE_FILE, _DIR or _BOTH
1719 char_u *rel_fname, // file name we are looking relative to.
1720 char_u *suffixes) // list of suffixes, 'suffixesadd' option
1721{
1722 static char_u *dir;
1723 static int did_findfile_init = FALSE;
1724 char_u save_char;
1725 char_u *file_name = NULL;
1726 char_u *buf = NULL;
1727 int rel_to_curdir;
1728# ifdef AMIGA
1729 struct Process *proc = (struct Process *)FindTask(0L);
1730 APTR save_winptr = proc->pr_WindowPtr;
1731
1732 // Avoid a requester here for a volume that doesn't exist.
1733 proc->pr_WindowPtr = (APTR)-1L;
1734# endif
1735
1736 if (first == TRUE)
1737 {
Bram Moolenaare015d992021-11-17 19:01:53 +00001738 if (len == 0)
1739 return NULL;
1740
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001741 // copy file name into NameBuff, expanding environment variables
1742 save_char = ptr[len];
1743 ptr[len] = NUL;
1744 expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
1745 ptr[len] = save_char;
1746
1747 vim_free(ff_file_to_find);
1748 ff_file_to_find = vim_strsave(NameBuff);
1749 if (ff_file_to_find == NULL) // out of memory
1750 {
1751 file_name = NULL;
1752 goto theend;
1753 }
1754 if (options & FNAME_UNESC)
1755 {
1756 // Change all "\ " to " ".
1757 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
1758 if (ptr[0] == '\\' && ptr[1] == ' ')
1759 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
1760 }
1761 }
1762
1763 rel_to_curdir = (ff_file_to_find[0] == '.'
1764 && (ff_file_to_find[1] == NUL
1765 || vim_ispathsep(ff_file_to_find[1])
1766 || (ff_file_to_find[1] == '.'
1767 && (ff_file_to_find[2] == NUL
1768 || vim_ispathsep(ff_file_to_find[2])))));
1769 if (vim_isAbsName(ff_file_to_find)
1770 // "..", "../path", "." and "./path": don't use the path_option
1771 || rel_to_curdir
1772# if defined(MSWIN)
1773 // handle "\tmp" as absolute path
1774 || vim_ispathsep(ff_file_to_find[0])
1775 // handle "c:name" as absolute path
1776 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
1777# endif
1778# ifdef AMIGA
1779 // handle ":tmp" as absolute path
1780 || ff_file_to_find[0] == ':'
1781# endif
1782 )
1783 {
1784 /*
1785 * Absolute path, no need to use "path_option".
1786 * If this is not a first call, return NULL. We already returned a
1787 * filename on the first call.
1788 */
1789 if (first == TRUE)
1790 {
1791 int l;
1792 int run;
1793
1794 if (path_with_url(ff_file_to_find))
1795 {
1796 file_name = vim_strsave(ff_file_to_find);
1797 goto theend;
1798 }
1799
1800 // When FNAME_REL flag given first use the directory of the file.
1801 // Otherwise or when this fails use the current directory.
1802 for (run = 1; run <= 2; ++run)
1803 {
1804 l = (int)STRLEN(ff_file_to_find);
1805 if (run == 1
1806 && rel_to_curdir
1807 && (options & FNAME_REL)
1808 && rel_fname != NULL
1809 && STRLEN(rel_fname) + l < MAXPATHL)
1810 {
1811 STRCPY(NameBuff, rel_fname);
1812 STRCPY(gettail(NameBuff), ff_file_to_find);
1813 l = (int)STRLEN(NameBuff);
1814 }
1815 else
1816 {
1817 STRCPY(NameBuff, ff_file_to_find);
1818 run = 2;
1819 }
1820
1821 // When the file doesn't exist, try adding parts of
1822 // 'suffixesadd'.
1823 buf = suffixes;
1824 for (;;)
1825 {
1826 if (mch_getperm(NameBuff) >= 0
1827 && (find_what == FINDFILE_BOTH
1828 || ((find_what == FINDFILE_DIR)
1829 == mch_isdir(NameBuff))))
1830 {
1831 file_name = vim_strsave(NameBuff);
1832 goto theend;
1833 }
1834 if (*buf == NUL)
1835 break;
1836 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
1837 }
1838 }
1839 }
1840 }
1841 else
1842 {
1843 /*
1844 * Loop over all paths in the 'path' or 'cdpath' option.
1845 * When "first" is set, first setup to the start of the option.
1846 * Otherwise continue to find the next match.
1847 */
1848 if (first == TRUE)
1849 {
1850 // vim_findfile_free_visited can handle a possible NULL pointer
1851 vim_findfile_free_visited(fdip_search_ctx);
1852 dir = path_option;
1853 did_findfile_init = FALSE;
1854 }
1855
1856 for (;;)
1857 {
1858 if (did_findfile_init)
1859 {
1860 file_name = vim_findfile(fdip_search_ctx);
1861 if (file_name != NULL)
1862 break;
1863
1864 did_findfile_init = FALSE;
1865 }
1866 else
1867 {
1868 char_u *r_ptr;
1869
1870 if (dir == NULL || *dir == NUL)
1871 {
1872 // We searched all paths of the option, now we can
1873 // free the search context.
1874 vim_findfile_cleanup(fdip_search_ctx);
1875 fdip_search_ctx = NULL;
1876 break;
1877 }
1878
Bram Moolenaar51e14382019-05-25 20:21:28 +02001879 if ((buf = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001880 break;
1881
1882 // copy next path
1883 buf[0] = 0;
1884 copy_option_part(&dir, buf, MAXPATHL, " ,");
1885
1886# ifdef FEAT_PATH_EXTRA
1887 // get the stopdir string
1888 r_ptr = vim_findfile_stopdir(buf);
1889# else
1890 r_ptr = NULL;
1891# endif
1892 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
1893 r_ptr, 100, FALSE, find_what,
1894 fdip_search_ctx, FALSE, rel_fname);
1895 if (fdip_search_ctx != NULL)
1896 did_findfile_init = TRUE;
1897 vim_free(buf);
1898 }
1899 }
1900 }
1901 if (file_name == NULL && (options & FNAME_MESS))
1902 {
1903 if (first == TRUE)
1904 {
1905 if (find_what == FINDFILE_DIR)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001906 semsg(_(e_cant_find_directory_str_in_cdpath),
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001907 ff_file_to_find);
1908 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001909 semsg(_(e_cant_find_file_str_in_path),
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001910 ff_file_to_find);
1911 }
1912 else
1913 {
1914 if (find_what == FINDFILE_DIR)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001915 semsg(_(e_no_more_directory_str_found_in_cdpath),
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001916 ff_file_to_find);
1917 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001918 semsg(_(e_no_more_file_str_found_in_path),
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001919 ff_file_to_find);
1920 }
1921 }
1922
1923theend:
1924# ifdef AMIGA
1925 proc->pr_WindowPtr = save_winptr;
1926# endif
1927 return file_name;
1928}
1929
1930/*
1931 * Get the file name at the cursor.
1932 * If Visual mode is active, use the selected text if it's in one line.
1933 * Returns the name in allocated memory, NULL for failure.
1934 */
1935 char_u *
1936grab_file_name(long count, linenr_T *file_lnum)
1937{
1938 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
1939
1940 if (VIsual_active)
1941 {
1942 int len;
1943 char_u *ptr;
1944
1945 if (get_visual_text(NULL, &ptr, &len) == FAIL)
1946 return NULL;
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +02001947 // Only recognize ":123" here
1948 if (file_lnum != NULL && ptr[len] == ':' && isdigit(ptr[len + 1]))
1949 {
1950 char_u *p = ptr + len + 1;
1951
1952 *file_lnum = getdigits(&p);
1953 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001954 return find_file_name_in_path(ptr, len, options,
1955 count, curbuf->b_ffname);
1956 }
1957 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
1958}
1959
1960/*
1961 * Return the file name under or after the cursor.
1962 *
1963 * The 'path' option is searched if the file name is not absolute.
1964 * The string returned has been alloc'ed and should be freed by the caller.
1965 * NULL is returned if the file name or file is not found.
1966 *
1967 * options:
1968 * FNAME_MESS give error messages
1969 * FNAME_EXP expand to path
1970 * FNAME_HYP check for hypertext link
1971 * FNAME_INCL apply "includeexpr"
1972 */
1973 char_u *
1974file_name_at_cursor(int options, long count, linenr_T *file_lnum)
1975{
1976 return file_name_in_line(ml_get_curline(),
1977 curwin->w_cursor.col, options, count, curbuf->b_ffname,
1978 file_lnum);
1979}
1980
1981/*
1982 * Return the name of the file under or after ptr[col].
1983 * Otherwise like file_name_at_cursor().
1984 */
1985 char_u *
1986file_name_in_line(
1987 char_u *line,
1988 int col,
1989 int options,
1990 long count,
1991 char_u *rel_fname, // file we are searching relative to
1992 linenr_T *file_lnum) // line number after the file name
1993{
1994 char_u *ptr;
1995 int len;
1996 int in_type = TRUE;
1997 int is_url = FALSE;
1998
1999 /*
2000 * search forward for what could be the start of a file name
2001 */
2002 ptr = line + col;
2003 while (*ptr != NUL && !vim_isfilec(*ptr))
2004 MB_PTR_ADV(ptr);
2005 if (*ptr == NUL) // nothing found
2006 {
2007 if (options & FNAME_MESS)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002008 emsg(_(e_no_file_name_under_cursor));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002009 return NULL;
2010 }
2011
2012 /*
2013 * Search backward for first char of the file name.
2014 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
2015 */
2016 while (ptr > line)
2017 {
2018 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
2019 ptr -= len + 1;
2020 else if (vim_isfilec(ptr[-1])
2021 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
2022 --ptr;
2023 else
2024 break;
2025 }
2026
2027 /*
2028 * Search forward for the last char of the file name.
2029 * Also allow "://" when ':' is not in 'isfname'.
2030 */
2031 len = 0;
2032 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
2033 || ((options & FNAME_HYP) && path_is_url(ptr + len))
Bram Moolenaarcbef8e12019-03-09 12:32:56 +01002034 || (is_url && vim_strchr((char_u *)":?&=", ptr[len]) != NULL))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002035 {
Bram Moolenaarcbef8e12019-03-09 12:32:56 +01002036 // After type:// we also include :, ?, & and = as valid characters, so that
2037 // http://google.com:8080?q=this&that=ok works.
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002038 if ((ptr[len] >= 'A' && ptr[len] <= 'Z') || (ptr[len] >= 'a' && ptr[len] <= 'z'))
2039 {
2040 if (in_type && path_is_url(ptr + len + 1))
2041 is_url = TRUE;
2042 }
2043 else
2044 in_type = FALSE;
2045
2046 if (ptr[len] == '\\')
2047 // Skip over the "\" in "\ ".
2048 ++len;
2049 if (has_mbyte)
2050 len += (*mb_ptr2len)(ptr + len);
2051 else
2052 ++len;
2053 }
2054
2055 /*
2056 * If there is trailing punctuation, remove it.
2057 * But don't remove "..", could be a directory name.
2058 */
2059 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
2060 && ptr[len - 2] != '.')
2061 --len;
2062
2063 if (file_lnum != NULL)
2064 {
2065 char_u *p;
Bram Moolenaar64e74c92019-12-22 15:38:06 +01002066 char *line_english = " line ";
2067 char *line_transl = _(line_msg);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002068
Bram Moolenaar64e74c92019-12-22 15:38:06 +01002069 // Get the number after the file name and a separator character.
2070 // Also accept " line 999" with and without the same translation as
2071 // used in last_set_msg().
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002072 p = ptr + len;
Bram Moolenaar64e74c92019-12-22 15:38:06 +01002073 if (STRNCMP(p, line_english, STRLEN(line_english)) == 0)
2074 p += STRLEN(line_english);
2075 else if (STRNCMP(p, line_transl, STRLEN(line_transl)) == 0)
2076 p += STRLEN(line_transl);
2077 else
2078 p = skipwhite(p);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002079 if (*p != NUL)
2080 {
2081 if (!isdigit(*p))
2082 ++p; // skip the separator
2083 p = skipwhite(p);
2084 if (isdigit(*p))
2085 *file_lnum = (int)getdigits(&p);
2086 }
2087 }
2088
2089 return find_file_name_in_path(ptr, len, options, count, rel_fname);
2090}
2091
2092# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2093 static char_u *
2094eval_includeexpr(char_u *ptr, int len)
2095{
2096 char_u *res;
2097
2098 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002099 res = eval_to_string_safe(curbuf->b_p_inex,
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002100 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
2101 set_vim_var_string(VV_FNAME, NULL, 0);
2102 return res;
2103}
2104# endif
2105
2106/*
2107 * Return the name of the file ptr[len] in 'path'.
2108 * Otherwise like file_name_at_cursor().
2109 */
2110 char_u *
2111find_file_name_in_path(
2112 char_u *ptr,
2113 int len,
2114 int options,
2115 long count,
2116 char_u *rel_fname) // file we are searching relative to
2117{
2118 char_u *file_name;
2119 int c;
2120# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2121 char_u *tofree = NULL;
Bram Moolenaar615ddd52021-11-17 18:00:31 +00002122# endif
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002123
Bram Moolenaar615ddd52021-11-17 18:00:31 +00002124 if (len == 0)
2125 return NULL;
2126
2127# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002128 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
2129 {
2130 tofree = eval_includeexpr(ptr, len);
2131 if (tofree != NULL)
2132 {
2133 ptr = tofree;
2134 len = (int)STRLEN(ptr);
2135 }
2136 }
2137# endif
2138
2139 if (options & FNAME_EXP)
2140 {
2141 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
2142 TRUE, rel_fname);
2143
2144# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2145 /*
2146 * If the file could not be found in a normal way, try applying
2147 * 'includeexpr' (unless done already).
2148 */
2149 if (file_name == NULL
2150 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
2151 {
2152 tofree = eval_includeexpr(ptr, len);
2153 if (tofree != NULL)
2154 {
2155 ptr = tofree;
2156 len = (int)STRLEN(ptr);
2157 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
2158 TRUE, rel_fname);
2159 }
2160 }
2161# endif
2162 if (file_name == NULL && (options & FNAME_MESS))
2163 {
2164 c = ptr[len];
2165 ptr[len] = NUL;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002166 semsg(_(e_cant_find_file_str_in_path_2), ptr);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002167 ptr[len] = c;
2168 }
2169
2170 // Repeat finding the file "count" times. This matters when it
2171 // appears several times in the path.
2172 while (file_name != NULL && --count > 0)
2173 {
2174 vim_free(file_name);
2175 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
2176 }
2177 }
2178 else
2179 file_name = vim_strnsave(ptr, len);
2180
2181# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2182 vim_free(tofree);
2183# endif
2184
2185 return file_name;
2186}
2187
2188/*
2189 * Return the end of the directory name, on the first path
2190 * separator:
2191 * "/path/file", "/path/dir/", "/path//dir", "/file"
2192 * ^ ^ ^ ^
2193 */
2194 static char_u *
2195gettail_dir(char_u *fname)
2196{
2197 char_u *dir_end = fname;
2198 char_u *next_dir_end = fname;
2199 int look_for_sep = TRUE;
2200 char_u *p;
2201
2202 for (p = fname; *p != NUL; )
2203 {
2204 if (vim_ispathsep(*p))
2205 {
2206 if (look_for_sep)
2207 {
2208 next_dir_end = p;
2209 look_for_sep = FALSE;
2210 }
2211 }
2212 else
2213 {
2214 if (!look_for_sep)
2215 dir_end = next_dir_end;
2216 look_for_sep = TRUE;
2217 }
2218 MB_PTR_ADV(p);
2219 }
2220 return dir_end;
2221}
2222
2223/*
2224 * return TRUE if 'c' is a path list separator.
2225 */
2226 int
2227vim_ispathlistsep(int c)
2228{
2229# ifdef UNIX
2230 return (c == ':');
2231# else
2232 return (c == ';'); // might not be right for every system...
2233# endif
2234}
2235
2236/*
2237 * Moves "*psep" back to the previous path separator in "path".
2238 * Returns FAIL is "*psep" ends up at the beginning of "path".
2239 */
2240 static int
2241find_previous_pathsep(char_u *path, char_u **psep)
2242{
2243 // skip the current separator
2244 if (*psep > path && vim_ispathsep(**psep))
2245 --*psep;
2246
2247 // find the previous separator
2248 while (*psep > path)
2249 {
2250 if (vim_ispathsep(**psep))
2251 return OK;
2252 MB_PTR_BACK(path, *psep);
2253 }
2254
2255 return FAIL;
2256}
2257
2258/*
2259 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
2260 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
2261 */
2262 static int
2263is_unique(char_u *maybe_unique, garray_T *gap, int i)
2264{
2265 int j;
2266 int candidate_len;
2267 int other_path_len;
2268 char_u **other_paths = (char_u **)gap->ga_data;
2269 char_u *rival;
2270
2271 for (j = 0; j < gap->ga_len; j++)
2272 {
2273 if (j == i)
2274 continue; // don't compare it with itself
2275
2276 candidate_len = (int)STRLEN(maybe_unique);
2277 other_path_len = (int)STRLEN(other_paths[j]);
2278 if (other_path_len < candidate_len)
2279 continue; // it's different when it's shorter
2280
2281 rival = other_paths[j] + other_path_len - candidate_len;
2282 if (fnamecmp(maybe_unique, rival) == 0
2283 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
2284 return FALSE; // match
2285 }
2286
2287 return TRUE; // no match found
2288}
2289
2290/*
2291 * Split the 'path' option into an array of strings in garray_T. Relative
2292 * paths are expanded to their equivalent fullpath. This includes the "."
2293 * (relative to current buffer directory) and empty path (relative to current
2294 * directory) notations.
2295 *
2296 * TODO: handle upward search (;) and path limiter (**N) notations by
2297 * expanding each into their equivalent path(s).
2298 */
2299 static void
2300expand_path_option(char_u *curdir, garray_T *gap)
2301{
2302 char_u *path_option = *curbuf->b_p_path == NUL
2303 ? p_path : curbuf->b_p_path;
2304 char_u *buf;
2305 char_u *p;
2306 int len;
2307
Bram Moolenaar51e14382019-05-25 20:21:28 +02002308 if ((buf = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002309 return;
2310
2311 while (*path_option != NUL)
2312 {
2313 copy_option_part(&path_option, buf, MAXPATHL, " ,");
2314
2315 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
2316 {
2317 // Relative to current buffer:
2318 // "/path/file" + "." -> "/path/"
2319 // "/path/file" + "./subdir" -> "/path/subdir"
2320 if (curbuf->b_ffname == NULL)
2321 continue;
2322 p = gettail(curbuf->b_ffname);
2323 len = (int)(p - curbuf->b_ffname);
2324 if (len + (int)STRLEN(buf) >= MAXPATHL)
2325 continue;
2326 if (buf[1] == NUL)
2327 buf[len] = NUL;
2328 else
2329 STRMOVE(buf + len, buf + 2);
2330 mch_memmove(buf, curbuf->b_ffname, len);
2331 simplify_filename(buf);
2332 }
2333 else if (buf[0] == NUL)
2334 // relative to current directory
2335 STRCPY(buf, curdir);
2336 else if (path_with_url(buf))
2337 // URL can't be used here
2338 continue;
2339 else if (!mch_isFullName(buf))
2340 {
2341 // Expand relative path to their full path equivalent
2342 len = (int)STRLEN(curdir);
2343 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
2344 continue;
2345 STRMOVE(buf + len + 1, buf);
2346 STRCPY(buf, curdir);
2347 buf[len] = PATHSEP;
2348 simplify_filename(buf);
2349 }
2350
2351 if (ga_grow(gap, 1) == FAIL)
2352 break;
2353
2354# if defined(MSWIN)
2355 // Avoid the path ending in a backslash, it fails when a comma is
2356 // appended.
2357 len = (int)STRLEN(buf);
2358 if (buf[len - 1] == '\\')
2359 buf[len - 1] = '/';
2360# endif
2361
2362 p = vim_strsave(buf);
2363 if (p == NULL)
2364 break;
2365 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
2366 }
2367
2368 vim_free(buf);
2369}
2370
2371/*
2372 * Returns a pointer to the file or directory name in "fname" that matches the
2373 * longest path in "ga"p, or NULL if there is no match. For example:
2374 *
2375 * path: /foo/bar/baz
2376 * fname: /foo/bar/baz/quux.txt
2377 * returns: ^this
2378 */
2379 static char_u *
2380get_path_cutoff(char_u *fname, garray_T *gap)
2381{
2382 int i;
2383 int maxlen = 0;
2384 char_u **path_part = (char_u **)gap->ga_data;
2385 char_u *cutoff = NULL;
2386
2387 for (i = 0; i < gap->ga_len; i++)
2388 {
2389 int j = 0;
2390
2391 while ((fname[j] == path_part[i][j]
2392# if defined(MSWIN)
2393 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
2394# endif
2395 ) && fname[j] != NUL && path_part[i][j] != NUL)
2396 j++;
2397 if (j > maxlen)
2398 {
2399 maxlen = j;
2400 cutoff = &fname[j];
2401 }
2402 }
2403
2404 // skip to the file or directory name
2405 if (cutoff != NULL)
2406 while (vim_ispathsep(*cutoff))
2407 MB_PTR_ADV(cutoff);
2408
2409 return cutoff;
2410}
2411
2412/*
2413 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
2414 * that they are unique with respect to each other while conserving the part
2415 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
2416 */
2417 void
2418uniquefy_paths(garray_T *gap, char_u *pattern)
2419{
2420 int i;
2421 int len;
2422 char_u **fnames = (char_u **)gap->ga_data;
2423 int sort_again = FALSE;
2424 char_u *pat;
2425 char_u *file_pattern;
2426 char_u *curdir;
2427 regmatch_T regmatch;
2428 garray_T path_ga;
2429 char_u **in_curdir = NULL;
2430 char_u *short_name;
2431
2432 remove_duplicates(gap);
2433 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
2434
2435 /*
2436 * We need to prepend a '*' at the beginning of file_pattern so that the
2437 * regex matches anywhere in the path. FIXME: is this valid for all
2438 * possible patterns?
2439 */
2440 len = (int)STRLEN(pattern);
2441 file_pattern = alloc(len + 2);
2442 if (file_pattern == NULL)
2443 return;
2444 file_pattern[0] = '*';
2445 file_pattern[1] = NUL;
2446 STRCAT(file_pattern, pattern);
2447 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
2448 vim_free(file_pattern);
2449 if (pat == NULL)
2450 return;
2451
2452 regmatch.rm_ic = TRUE; // always ignore case
2453 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2454 vim_free(pat);
2455 if (regmatch.regprog == NULL)
2456 return;
2457
Bram Moolenaar51e14382019-05-25 20:21:28 +02002458 if ((curdir = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002459 goto theend;
2460 mch_dirname(curdir, MAXPATHL);
2461 expand_path_option(curdir, &path_ga);
2462
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002463 in_curdir = ALLOC_CLEAR_MULT(char_u *, gap->ga_len);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002464 if (in_curdir == NULL)
2465 goto theend;
2466
2467 for (i = 0; i < gap->ga_len && !got_int; i++)
2468 {
2469 char_u *path = fnames[i];
2470 int is_in_curdir;
2471 char_u *dir_end = gettail_dir(path);
2472 char_u *pathsep_p;
2473 char_u *path_cutoff;
2474
2475 len = (int)STRLEN(path);
2476 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
2477 && curdir[dir_end - path] == NUL;
2478 if (is_in_curdir)
2479 in_curdir[i] = vim_strsave(path);
2480
2481 // Shorten the filename while maintaining its uniqueness
2482 path_cutoff = get_path_cutoff(path, &path_ga);
2483
2484 // Don't assume all files can be reached without path when search
2485 // pattern starts with star star slash, so only remove path_cutoff
2486 // when possible.
2487 if (pattern[0] == '*' && pattern[1] == '*'
2488 && vim_ispathsep_nocolon(pattern[2])
2489 && path_cutoff != NULL
2490 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
2491 && is_unique(path_cutoff, gap, i))
2492 {
2493 sort_again = TRUE;
2494 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
2495 }
2496 else
2497 {
2498 // Here all files can be reached without path, so get shortest
2499 // unique path. We start at the end of the path.
2500 pathsep_p = path + len - 1;
2501
2502 while (find_previous_pathsep(path, &pathsep_p))
2503 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
2504 && is_unique(pathsep_p + 1, gap, i)
2505 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
2506 {
2507 sort_again = TRUE;
2508 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
2509 break;
2510 }
2511 }
2512
2513 if (mch_isFullName(path))
2514 {
2515 /*
2516 * Last resort: shorten relative to curdir if possible.
2517 * 'possible' means:
2518 * 1. It is under the current directory.
2519 * 2. The result is actually shorter than the original.
2520 *
2521 * Before curdir After
2522 * /foo/bar/file.txt /foo/bar ./file.txt
2523 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
2524 * /file.txt / /file.txt
2525 * c:\file.txt c:\ .\file.txt
2526 */
2527 short_name = shorten_fname(path, curdir);
2528 if (short_name != NULL && short_name > path + 1
2529# if defined(MSWIN)
2530 // On windows,
2531 // shorten_fname("c:\a\a.txt", "c:\a\b")
2532 // returns "\a\a.txt", which is not really the short
2533 // name, hence:
2534 && !vim_ispathsep(*short_name)
2535# endif
2536 )
2537 {
2538 STRCPY(path, ".");
2539 add_pathsep(path);
2540 STRMOVE(path + STRLEN(path), short_name);
2541 }
2542 }
2543 ui_breakcheck();
2544 }
2545
2546 // Shorten filenames in /in/current/directory/{filename}
2547 for (i = 0; i < gap->ga_len && !got_int; i++)
2548 {
2549 char_u *rel_path;
2550 char_u *path = in_curdir[i];
2551
2552 if (path == NULL)
2553 continue;
2554
2555 // If the {filename} is not unique, change it to ./{filename}.
2556 // Else reduce it to {filename}
2557 short_name = shorten_fname(path, curdir);
2558 if (short_name == NULL)
2559 short_name = path;
2560 if (is_unique(short_name, gap, i))
2561 {
2562 STRCPY(fnames[i], short_name);
2563 continue;
2564 }
2565
Bram Moolenaar51e14382019-05-25 20:21:28 +02002566 rel_path = alloc(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002567 if (rel_path == NULL)
2568 goto theend;
2569 STRCPY(rel_path, ".");
2570 add_pathsep(rel_path);
2571 STRCAT(rel_path, short_name);
2572
2573 vim_free(fnames[i]);
2574 fnames[i] = rel_path;
2575 sort_again = TRUE;
2576 ui_breakcheck();
2577 }
2578
2579theend:
2580 vim_free(curdir);
2581 if (in_curdir != NULL)
2582 {
2583 for (i = 0; i < gap->ga_len; i++)
2584 vim_free(in_curdir[i]);
2585 vim_free(in_curdir);
2586 }
2587 ga_clear_strings(&path_ga);
2588 vim_regfree(regmatch.regprog);
2589
2590 if (sort_again)
2591 remove_duplicates(gap);
2592}
2593
2594/*
2595 * Calls globpath() with 'path' values for the given pattern and stores the
2596 * result in "gap".
2597 * Returns the total number of matches.
2598 */
2599 int
2600expand_in_path(
2601 garray_T *gap,
2602 char_u *pattern,
2603 int flags) // EW_* flags
2604{
2605 char_u *curdir;
2606 garray_T path_ga;
2607 char_u *paths = NULL;
2608 int glob_flags = 0;
2609
Bram Moolenaar964b3742019-05-24 18:54:09 +02002610 if ((curdir = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002611 return 0;
2612 mch_dirname(curdir, MAXPATHL);
2613
2614 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
2615 expand_path_option(curdir, &path_ga);
2616 vim_free(curdir);
2617 if (path_ga.ga_len == 0)
2618 return 0;
2619
2620 paths = ga_concat_strings(&path_ga, ",");
2621 ga_clear_strings(&path_ga);
2622 if (paths == NULL)
2623 return 0;
2624
2625 if (flags & EW_ICASE)
2626 glob_flags |= WILD_ICASE;
2627 if (flags & EW_ADDSLASH)
2628 glob_flags |= WILD_ADD_SLASH;
2629 globpath(paths, pattern, gap, glob_flags);
2630 vim_free(paths);
2631
2632 return gap->ga_len;
2633}
2634
2635#endif // FEAT_SEARCHPATH
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002636
2637/*
2638 * Converts a file name into a canonical form. It simplifies a file name into
2639 * its simplest form by stripping out unneeded components, if any. The
2640 * resulting file name is simplified in place and will either be the same
2641 * length as that supplied, or shorter.
2642 */
2643 void
2644simplify_filename(char_u *filename)
2645{
2646#ifndef AMIGA // Amiga doesn't have "..", it uses "/"
2647 int components = 0;
2648 char_u *p, *tail, *start;
2649 int stripping_disabled = FALSE;
2650 int relative = TRUE;
2651
2652 p = filename;
2653# ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmanan6df0f272021-12-16 13:06:10 +00002654 if (p[0] != NUL && p[1] == ':') // skip "x:"
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002655 p += 2;
2656# endif
2657
2658 if (vim_ispathsep(*p))
2659 {
2660 relative = FALSE;
2661 do
2662 ++p;
2663 while (vim_ispathsep(*p));
2664 }
2665 start = p; // remember start after "c:/" or "/" or "///"
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02002666#ifdef UNIX
2667 // Posix says that "//path" is unchanged but "///path" is "/path".
2668 if (start > filename + 2)
2669 {
2670 STRMOVE(filename + 1, p);
2671 start = p = filename + 1;
2672 }
2673#endif
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002674
2675 do
2676 {
2677 // At this point "p" is pointing to the char following a single "/"
2678 // or "p" is at the "start" of the (absolute or relative) path name.
2679# ifdef VMS
2680 // VMS allows device:[path] - don't strip the [ in directory
2681 if ((*p == '[' || *p == '<') && p > filename && p[-1] == ':')
2682 {
2683 // :[ or :< composition: vms directory component
2684 ++components;
2685 p = getnextcomp(p + 1);
2686 }
2687 // allow remote calls as host"user passwd"::device:[path]
2688 else if (p[0] == ':' && p[1] == ':' && p > filename && p[-1] == '"' )
2689 {
2690 // ":: composition: vms host/passwd component
2691 ++components;
2692 p = getnextcomp(p + 2);
2693 }
2694 else
2695# endif
2696 if (vim_ispathsep(*p))
2697 STRMOVE(p, p + 1); // remove duplicate "/"
2698 else if (p[0] == '.' && (vim_ispathsep(p[1]) || p[1] == NUL))
2699 {
2700 if (p == start && relative)
2701 p += 1 + (p[1] != NUL); // keep single "." or leading "./"
2702 else
2703 {
2704 // Strip "./" or ".///". If we are at the end of the file name
2705 // and there is no trailing path separator, either strip "/." if
2706 // we are after "start", or strip "." if we are at the beginning
2707 // of an absolute path name .
2708 tail = p + 1;
2709 if (p[1] != NUL)
2710 while (vim_ispathsep(*tail))
2711 MB_PTR_ADV(tail);
2712 else if (p > start)
2713 --p; // strip preceding path separator
2714 STRMOVE(p, tail);
2715 }
2716 }
2717 else if (p[0] == '.' && p[1] == '.' &&
2718 (vim_ispathsep(p[2]) || p[2] == NUL))
2719 {
2720 // Skip to after ".." or "../" or "..///".
2721 tail = p + 2;
2722 while (vim_ispathsep(*tail))
2723 MB_PTR_ADV(tail);
2724
2725 if (components > 0) // strip one preceding component
2726 {
2727 int do_strip = FALSE;
2728 char_u saved_char;
2729 stat_T st;
2730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002731 // Don't strip for an erroneous file name.
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002732 if (!stripping_disabled)
2733 {
2734 // If the preceding component does not exist in the file
2735 // system, we strip it. On Unix, we don't accept a symbolic
2736 // link that refers to a non-existent file.
2737 saved_char = p[-1];
2738 p[-1] = NUL;
2739# ifdef UNIX
2740 if (mch_lstat((char *)filename, &st) < 0)
2741# else
2742 if (mch_stat((char *)filename, &st) < 0)
2743# endif
2744 do_strip = TRUE;
2745 p[-1] = saved_char;
2746
2747 --p;
2748 // Skip back to after previous '/'.
2749 while (p > start && !after_pathsep(start, p))
2750 MB_PTR_BACK(start, p);
2751
2752 if (!do_strip)
2753 {
2754 // If the component exists in the file system, check
2755 // that stripping it won't change the meaning of the
2756 // file name. First get information about the
2757 // unstripped file name. This may fail if the component
2758 // to strip is not a searchable directory (but a regular
2759 // file, for instance), since the trailing "/.." cannot
2760 // be applied then. We don't strip it then since we
2761 // don't want to replace an erroneous file name by
2762 // a valid one, and we disable stripping of later
2763 // components.
2764 saved_char = *tail;
2765 *tail = NUL;
2766 if (mch_stat((char *)filename, &st) >= 0)
2767 do_strip = TRUE;
2768 else
2769 stripping_disabled = TRUE;
2770 *tail = saved_char;
2771# ifdef UNIX
2772 if (do_strip)
2773 {
2774 stat_T new_st;
2775
2776 // On Unix, the check for the unstripped file name
2777 // above works also for a symbolic link pointing to
2778 // a searchable directory. But then the parent of
2779 // the directory pointed to by the link must be the
2780 // same as the stripped file name. (The latter
2781 // exists in the file system since it is the
2782 // component's parent directory.)
2783 if (p == start && relative)
2784 (void)mch_stat(".", &new_st);
2785 else
2786 {
2787 saved_char = *p;
2788 *p = NUL;
2789 (void)mch_stat((char *)filename, &new_st);
2790 *p = saved_char;
2791 }
2792
2793 if (new_st.st_ino != st.st_ino ||
2794 new_st.st_dev != st.st_dev)
2795 {
2796 do_strip = FALSE;
2797 // We don't disable stripping of later
2798 // components since the unstripped path name is
2799 // still valid.
2800 }
2801 }
2802# endif
2803 }
2804 }
2805
2806 if (!do_strip)
2807 {
2808 // Skip the ".." or "../" and reset the counter for the
2809 // components that might be stripped later on.
2810 p = tail;
2811 components = 0;
2812 }
2813 else
2814 {
2815 // Strip previous component. If the result would get empty
2816 // and there is no trailing path separator, leave a single
2817 // "." instead. If we are at the end of the file name and
2818 // there is no trailing path separator and a preceding
2819 // component is left after stripping, strip its trailing
2820 // path separator as well.
2821 if (p == start && relative && tail[-1] == '.')
2822 {
2823 *p++ = '.';
2824 *p = NUL;
2825 }
2826 else
2827 {
2828 if (p > start && tail[-1] == '.')
2829 --p;
2830 STRMOVE(p, tail); // strip previous component
2831 }
2832
2833 --components;
2834 }
2835 }
2836 else if (p == start && !relative) // leading "/.." or "/../"
2837 STRMOVE(p, tail); // strip ".." or "../"
2838 else
2839 {
2840 if (p == start + 2 && p[-2] == '.') // leading "./../"
2841 {
2842 STRMOVE(p - 2, p); // strip leading "./"
2843 tail -= 2;
2844 }
2845 p = tail; // skip to char after ".." or "../"
2846 }
2847 }
2848 else
2849 {
2850 ++components; // simple path component
2851 p = getnextcomp(p);
2852 }
2853 } while (*p != NUL);
2854#endif // !AMIGA
2855}
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002856
2857#if defined(FEAT_EVAL) || defined(PROTO)
2858/*
2859 * "simplify()" function
2860 */
2861 void
2862f_simplify(typval_T *argvars, typval_T *rettv)
2863{
2864 char_u *p;
2865
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002866 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2867 return;
2868
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02002869 p = tv_get_string_strict(&argvars[0]);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002870 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002871 simplify_filename(rettv->vval.v_string); // simplify in place
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002872 rettv->v_type = VAR_STRING;
2873}
2874#endif // FEAT_EVAL