blob: 8267be6e0951a799244a7830732d8d6e5ca06f74 [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;
John Drouhard95fca122022-08-01 11:38:17 +010078 int ffs_filearray_cur; // needed for partly handled dirs
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010079
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).
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100492 * If no restrict is given after '**' the default is used.
493 * Due to this technique the path looks awful if you print it as a
494 * string.
495 */
496 len = 0;
497 while (*wc_part != NUL)
498 {
499 if (len + 5 >= MAXPATHL)
500 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000501 emsg(_(e_path_too_long_for_completion));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100502 break;
503 }
504 if (STRNCMP(wc_part, "**", 2) == 0)
505 {
506 ff_expand_buffer[len++] = *wc_part++;
507 ff_expand_buffer[len++] = *wc_part++;
508
509 llevel = strtol((char *)wc_part, &errpt, 10);
510 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
511 ff_expand_buffer[len++] = llevel;
512 else if ((char_u *)errpt != wc_part && llevel == 0)
513 // restrict is 0 -> remove already added '**'
514 len -= 2;
515 else
516 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
517 wc_part = (char_u *)errpt;
518 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
519 {
Bram Moolenaareaaac012022-01-02 17:00:40 +0000520 semsg(_(e_invalid_path_number_must_be_at_end_of_path_or_be_followed_by_str), PATHSEPSTR);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100521 goto error_return;
522 }
523 }
524 else
525 ff_expand_buffer[len++] = *wc_part++;
526 }
527 ff_expand_buffer[len] = NUL;
528 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
529
530 if (search_ctx->ffsc_wc_path == NULL)
531 goto error_return;
532 }
533 else
534#endif
535 search_ctx->ffsc_fix_path = vim_strsave(path);
536
537 if (search_ctx->ffsc_start_dir == NULL)
538 {
539 // store the fix part as startdir.
540 // This is needed if the parameter path is fully qualified.
541 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
542 if (search_ctx->ffsc_start_dir == NULL)
543 goto error_return;
544 search_ctx->ffsc_fix_path[0] = NUL;
545 }
546
547 // create an absolute path
548 if (STRLEN(search_ctx->ffsc_start_dir)
549 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
550 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000551 emsg(_(e_path_too_long_for_completion));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100552 goto error_return;
553 }
554 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
555 add_pathsep(ff_expand_buffer);
556 {
557 int eb_len = (int)STRLEN(ff_expand_buffer);
558 char_u *buf = alloc(eb_len
559 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
560
561 STRCPY(buf, ff_expand_buffer);
562 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
563 if (mch_isdir(buf))
564 {
565 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
566 add_pathsep(ff_expand_buffer);
567 }
568#ifdef FEAT_PATH_EXTRA
569 else
570 {
571 char_u *p = gettail(search_ctx->ffsc_fix_path);
572 char_u *wc_path = NULL;
573 char_u *temp = NULL;
574 int len = 0;
575
576 if (p > search_ctx->ffsc_fix_path)
577 {
Christian Brabandt7a4ca322021-07-25 15:08:05 +0200578 // do not add '..' to the path and start upwards searching
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100579 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Christian Brabandt7a4ca322021-07-25 15:08:05 +0200580 if ((len >= 2
581 && STRNCMP(search_ctx->ffsc_fix_path, "..", 2) == 0)
582 && (len == 2
583 || search_ctx->ffsc_fix_path[2] == PATHSEP))
584 {
585 vim_free(buf);
586 goto error_return;
587 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100588 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
589 add_pathsep(ff_expand_buffer);
590 }
591 else
592 len = (int)STRLEN(search_ctx->ffsc_fix_path);
593
594 if (search_ctx->ffsc_wc_path != NULL)
595 {
596 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar51e14382019-05-25 20:21:28 +0200597 temp = alloc(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100598 + STRLEN(search_ctx->ffsc_fix_path + len)
Bram Moolenaar51e14382019-05-25 20:21:28 +0200599 + 1);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100600 if (temp == NULL || wc_path == NULL)
601 {
602 vim_free(buf);
603 vim_free(temp);
604 vim_free(wc_path);
605 goto error_return;
606 }
607
608 STRCPY(temp, search_ctx->ffsc_fix_path + len);
609 STRCAT(temp, search_ctx->ffsc_wc_path);
610 vim_free(search_ctx->ffsc_wc_path);
611 vim_free(wc_path);
612 search_ctx->ffsc_wc_path = temp;
613 }
614 }
615#endif
616 vim_free(buf);
617 }
618
619 sptr = ff_create_stack_element(ff_expand_buffer,
620#ifdef FEAT_PATH_EXTRA
621 search_ctx->ffsc_wc_path,
622#endif
623 level, 0);
624
625 if (sptr == NULL)
626 goto error_return;
627
628 ff_push(search_ctx, sptr);
629
630 search_ctx->ffsc_file_to_search = vim_strsave(filename);
631 if (search_ctx->ffsc_file_to_search == NULL)
632 goto error_return;
633
634 return search_ctx;
635
636error_return:
637 /*
638 * We clear the search context now!
639 * Even when the caller gave us a (perhaps valid) context we free it here,
640 * as we might have already destroyed it.
641 */
642 vim_findfile_cleanup(search_ctx);
643 return NULL;
644}
645
646#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
647/*
648 * Get the stopdir string. Check that ';' is not escaped.
649 */
650 char_u *
651vim_findfile_stopdir(char_u *buf)
652{
653 char_u *r_ptr = buf;
654
655 while (*r_ptr != NUL && *r_ptr != ';')
656 {
657 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
658 {
659 // Overwrite the escape char,
660 // use STRLEN(r_ptr) to move the trailing '\0'.
661 STRMOVE(r_ptr, r_ptr + 1);
662 r_ptr++;
663 }
664 r_ptr++;
665 }
666 if (*r_ptr == ';')
667 {
668 *r_ptr = 0;
669 r_ptr++;
670 }
671 else if (*r_ptr == NUL)
672 r_ptr = NULL;
673 return r_ptr;
674}
675#endif
676
677/*
678 * Clean up the given search context. Can handle a NULL pointer.
679 */
680 void
681vim_findfile_cleanup(void *ctx)
682{
683 if (ctx == NULL)
684 return;
685
686 vim_findfile_free_visited(ctx);
687 ff_clear(ctx);
688 vim_free(ctx);
689}
690
691/*
692 * Find a file in a search context.
693 * The search context was created with vim_findfile_init() above.
694 * Return a pointer to an allocated file name or NULL if nothing found.
695 * To get all matching files call this function until you get NULL.
696 *
697 * If the passed search_context is NULL, NULL is returned.
698 *
699 * The search algorithm is depth first. To change this replace the
700 * stack with a list (don't forget to leave partly searched directories on the
701 * top of the list).
702 */
703 char_u *
704vim_findfile(void *search_ctx_arg)
705{
706 char_u *file_path;
707#ifdef FEAT_PATH_EXTRA
708 char_u *rest_of_wildcards;
709 char_u *path_end = NULL;
710#endif
711 ff_stack_T *stackp;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100712 int len;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100713 int i;
714 char_u *p;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100715 char_u *suf;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100716 ff_search_ctx_T *search_ctx;
717
718 if (search_ctx_arg == NULL)
719 return NULL;
720
721 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
722
723 /*
724 * filepath is used as buffer for various actions and as the storage to
725 * return a found filename.
726 */
Bram Moolenaar51e14382019-05-25 20:21:28 +0200727 if ((file_path = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100728 return NULL;
729
730#ifdef FEAT_PATH_EXTRA
731 // store the end of the start dir -- needed for upward search
732 if (search_ctx->ffsc_start_dir != NULL)
733 path_end = &search_ctx->ffsc_start_dir[
734 STRLEN(search_ctx->ffsc_start_dir)];
735#endif
736
737#ifdef FEAT_PATH_EXTRA
738 // upward search loop
739 for (;;)
740 {
741#endif
742 // downward search loop
743 for (;;)
744 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000745 // check if user wants to stop the search
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100746 ui_breakcheck();
747 if (got_int)
748 break;
749
750 // get directory to work on from stack
751 stackp = ff_pop(search_ctx);
752 if (stackp == NULL)
753 break;
754
755 /*
756 * TODO: decide if we leave this test in
757 *
758 * GOOD: don't search a directory(-tree) twice.
759 * BAD: - check linked list for every new directory entered.
760 * - check for double files also done below
761 *
762 * Here we check if we already searched this directory.
763 * We already searched a directory if:
764 * 1) The directory is the same.
765 * 2) We would use the same wildcard string.
766 *
767 * Good if you have links on same directory via several ways
768 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
769 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
770 *
771 * This check is only needed for directories we work on for the
772 * first time (hence stackp->ff_filearray == NULL)
773 */
774 if (stackp->ffs_filearray == NULL
775 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
776 ->ffvl_visited_list,
777 stackp->ffs_fix_path
778#ifdef FEAT_PATH_EXTRA
779 , stackp->ffs_wc_path
780#endif
781 ) == FAIL)
782 {
783#ifdef FF_VERBOSE
784 if (p_verbose >= 5)
785 {
786 verbose_enter_scroll();
787 smsg("Already Searched: %s (%s)",
788 stackp->ffs_fix_path, stackp->ffs_wc_path);
789 // don't overwrite this either
790 msg_puts("\n");
791 verbose_leave_scroll();
792 }
793#endif
794 ff_free_stack_element(stackp);
795 continue;
796 }
797#ifdef FF_VERBOSE
798 else if (p_verbose >= 5)
799 {
800 verbose_enter_scroll();
801 smsg("Searching: %s (%s)",
802 stackp->ffs_fix_path, stackp->ffs_wc_path);
803 // don't overwrite this either
804 msg_puts("\n");
805 verbose_leave_scroll();
806 }
807#endif
808
809 // check depth
810 if (stackp->ffs_level <= 0)
811 {
812 ff_free_stack_element(stackp);
813 continue;
814 }
815
816 file_path[0] = NUL;
817
818 /*
819 * If no filearray till now expand wildcards
820 * The function expand_wildcards() can handle an array of paths
821 * and all possible expands are returned in one array. We use this
822 * to handle the expansion of '**' into an empty string.
823 */
824 if (stackp->ffs_filearray == NULL)
825 {
826 char_u *dirptrs[2];
827
828 // we use filepath to build the path expand_wildcards() should
829 // expand.
830 dirptrs[0] = file_path;
831 dirptrs[1] = NULL;
832
833 // if we have a start dir copy it in
834 if (!vim_isAbsName(stackp->ffs_fix_path)
835 && search_ctx->ffsc_start_dir)
836 {
837 if (STRLEN(search_ctx->ffsc_start_dir) + 1 < MAXPATHL)
838 {
839 STRCPY(file_path, search_ctx->ffsc_start_dir);
840 add_pathsep(file_path);
841 }
842 else
843 {
844 ff_free_stack_element(stackp);
845 goto fail;
846 }
847 }
848
849 // append the fix part of the search path
850 if (STRLEN(file_path) + STRLEN(stackp->ffs_fix_path) + 1
851 < MAXPATHL)
852 {
853 STRCAT(file_path, stackp->ffs_fix_path);
854 add_pathsep(file_path);
855 }
856 else
857 {
858 ff_free_stack_element(stackp);
859 goto fail;
860 }
861
862#ifdef FEAT_PATH_EXTRA
863 rest_of_wildcards = stackp->ffs_wc_path;
864 if (*rest_of_wildcards != NUL)
865 {
866 len = (int)STRLEN(file_path);
867 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
868 {
869 // pointer to the restrict byte
870 // The restrict byte is not a character!
871 p = rest_of_wildcards + 2;
872
873 if (*p > 0)
874 {
875 (*p)--;
876 if (len + 1 < MAXPATHL)
877 file_path[len++] = '*';
878 else
879 {
880 ff_free_stack_element(stackp);
881 goto fail;
882 }
883 }
884
885 if (*p == 0)
886 {
887 // remove '**<numb> from wildcards
888 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
889 }
890 else
891 rest_of_wildcards += 3;
892
893 if (stackp->ffs_star_star_empty == 0)
894 {
895 // if not done before, expand '**' to empty
896 stackp->ffs_star_star_empty = 1;
897 dirptrs[1] = stackp->ffs_fix_path;
898 }
899 }
900
901 /*
902 * Here we copy until the next path separator or the end of
903 * the path. If we stop at a path separator, there is
904 * still something else left. This is handled below by
905 * pushing every directory returned from expand_wildcards()
906 * on the stack again for further search.
907 */
908 while (*rest_of_wildcards
909 && !vim_ispathsep(*rest_of_wildcards))
910 if (len + 1 < MAXPATHL)
911 file_path[len++] = *rest_of_wildcards++;
912 else
913 {
914 ff_free_stack_element(stackp);
915 goto fail;
916 }
917
918 file_path[len] = NUL;
919 if (vim_ispathsep(*rest_of_wildcards))
920 rest_of_wildcards++;
921 }
922#endif
923
924 /*
925 * Expand wildcards like "*" and "$VAR".
926 * If the path is a URL don't try this.
927 */
928 if (path_with_url(dirptrs[0]))
929 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200930 stackp->ffs_filearray = ALLOC_ONE(char_u *);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100931 if (stackp->ffs_filearray != NULL
932 && (stackp->ffs_filearray[0]
933 = vim_strsave(dirptrs[0])) != NULL)
934 stackp->ffs_filearray_size = 1;
935 else
936 stackp->ffs_filearray_size = 0;
937 }
938 else
939 // Add EW_NOTWILD because the expanded path may contain
940 // wildcard characters that are to be taken literally.
941 // This is a bit of a hack.
942 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
943 &stackp->ffs_filearray_size,
944 &stackp->ffs_filearray,
945 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
946
947 stackp->ffs_filearray_cur = 0;
948 stackp->ffs_stage = 0;
949 }
950#ifdef FEAT_PATH_EXTRA
951 else
952 rest_of_wildcards = &stackp->ffs_wc_path[
953 STRLEN(stackp->ffs_wc_path)];
954#endif
955
956 if (stackp->ffs_stage == 0)
957 {
958 // this is the first time we work on this directory
959#ifdef FEAT_PATH_EXTRA
960 if (*rest_of_wildcards == NUL)
961#endif
962 {
963 /*
964 * We don't have further wildcards to expand, so we have to
965 * check for the final file now.
966 */
967 for (i = stackp->ffs_filearray_cur;
968 i < stackp->ffs_filearray_size; ++i)
969 {
970 if (!path_with_url(stackp->ffs_filearray[i])
971 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100972 continue; // not a directory
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100973
974 // prepare the filename to be checked for existence
975 // below
976 if (STRLEN(stackp->ffs_filearray[i]) + 1
977 + STRLEN(search_ctx->ffsc_file_to_search)
978 < MAXPATHL)
979 {
980 STRCPY(file_path, stackp->ffs_filearray[i]);
981 add_pathsep(file_path);
982 STRCAT(file_path, search_ctx->ffsc_file_to_search);
983 }
984 else
985 {
986 ff_free_stack_element(stackp);
987 goto fail;
988 }
989
990 /*
991 * Try without extra suffix and then with suffixes
992 * from 'suffixesadd'.
993 */
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100994 len = (int)STRLEN(file_path);
995 if (search_ctx->ffsc_tagfile)
996 suf = (char_u *)"";
997 else
998 suf = curbuf->b_p_sua;
999 for (;;)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001000 {
1001 // if file exists and we didn't already find it
1002 if ((path_with_url(file_path)
1003 || (mch_getperm(file_path) >= 0
1004 && (search_ctx->ffsc_find_what
1005 == FINDFILE_BOTH
1006 || ((search_ctx->ffsc_find_what
1007 == FINDFILE_DIR)
1008 == mch_isdir(file_path)))))
1009#ifndef FF_VERBOSE
1010 && (ff_check_visited(
1011 &search_ctx->ffsc_visited_list->ffvl_visited_list,
1012 file_path
1013#ifdef FEAT_PATH_EXTRA
1014 , (char_u *)""
1015#endif
1016 ) == OK)
1017#endif
1018 )
1019 {
1020#ifdef FF_VERBOSE
1021 if (ff_check_visited(
1022 &search_ctx->ffsc_visited_list->ffvl_visited_list,
1023 file_path
1024#ifdef FEAT_PATH_EXTRA
1025 , (char_u *)""
1026#endif
1027 ) == FAIL)
1028 {
1029 if (p_verbose >= 5)
1030 {
1031 verbose_enter_scroll();
1032 smsg("Already: %s",
1033 file_path);
1034 // don't overwrite this either
1035 msg_puts("\n");
1036 verbose_leave_scroll();
1037 }
1038 continue;
1039 }
1040#endif
1041
1042 // push dir to examine rest of subdirs later
1043 stackp->ffs_filearray_cur = i + 1;
1044 ff_push(search_ctx, stackp);
1045
1046 if (!path_with_url(file_path))
1047 simplify_filename(file_path);
1048 if (mch_dirname(ff_expand_buffer, MAXPATHL)
1049 == OK)
1050 {
1051 p = shorten_fname(file_path,
1052 ff_expand_buffer);
1053 if (p != NULL)
1054 STRMOVE(file_path, p);
1055 }
1056#ifdef FF_VERBOSE
1057 if (p_verbose >= 5)
1058 {
1059 verbose_enter_scroll();
1060 smsg("HIT: %s", file_path);
1061 // don't overwrite this either
1062 msg_puts("\n");
1063 verbose_leave_scroll();
1064 }
1065#endif
1066 return file_path;
1067 }
1068
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001069 // Not found or found already, try next suffix.
1070 if (*suf == NUL)
1071 break;
1072 copy_option_part(&suf, file_path + len,
1073 MAXPATHL - len, ",");
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001074 }
1075 }
1076 }
1077#ifdef FEAT_PATH_EXTRA
1078 else
1079 {
1080 /*
1081 * still wildcards left, push the directories for further
1082 * search
1083 */
1084 for (i = stackp->ffs_filearray_cur;
1085 i < stackp->ffs_filearray_size; ++i)
1086 {
1087 if (!mch_isdir(stackp->ffs_filearray[i]))
1088 continue; // not a directory
1089
1090 ff_push(search_ctx,
1091 ff_create_stack_element(
1092 stackp->ffs_filearray[i],
1093 rest_of_wildcards,
1094 stackp->ffs_level - 1, 0));
1095 }
1096 }
1097#endif
1098 stackp->ffs_filearray_cur = 0;
1099 stackp->ffs_stage = 1;
1100 }
1101
1102#ifdef FEAT_PATH_EXTRA
1103 /*
1104 * if wildcards contains '**' we have to descent till we reach the
1105 * leaves of the directory tree.
1106 */
1107 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
1108 {
1109 for (i = stackp->ffs_filearray_cur;
1110 i < stackp->ffs_filearray_size; ++i)
1111 {
1112 if (fnamecmp(stackp->ffs_filearray[i],
1113 stackp->ffs_fix_path) == 0)
1114 continue; // don't repush same directory
1115 if (!mch_isdir(stackp->ffs_filearray[i]))
1116 continue; // not a directory
1117 ff_push(search_ctx,
1118 ff_create_stack_element(stackp->ffs_filearray[i],
1119 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
1120 }
1121 }
1122#endif
1123
1124 // we are done with the current directory
1125 ff_free_stack_element(stackp);
1126
1127 }
1128
1129#ifdef FEAT_PATH_EXTRA
1130 // If we reached this, we didn't find anything downwards.
1131 // Let's check if we should do an upward search.
1132 if (search_ctx->ffsc_start_dir
1133 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
1134 {
1135 ff_stack_T *sptr;
1136
1137 // is the last starting directory in the stop list?
1138 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
1139 (int)(path_end - search_ctx->ffsc_start_dir),
1140 search_ctx->ffsc_stopdirs_v) == TRUE)
1141 break;
1142
1143 // cut of last dir
1144 while (path_end > search_ctx->ffsc_start_dir
1145 && vim_ispathsep(*path_end))
1146 path_end--;
1147 while (path_end > search_ctx->ffsc_start_dir
1148 && !vim_ispathsep(path_end[-1]))
1149 path_end--;
1150 *path_end = 0;
1151 path_end--;
1152
1153 if (*search_ctx->ffsc_start_dir == 0)
1154 break;
1155
1156 if (STRLEN(search_ctx->ffsc_start_dir) + 1
1157 + STRLEN(search_ctx->ffsc_fix_path) < MAXPATHL)
1158 {
1159 STRCPY(file_path, search_ctx->ffsc_start_dir);
1160 add_pathsep(file_path);
1161 STRCAT(file_path, search_ctx->ffsc_fix_path);
1162 }
1163 else
1164 goto fail;
1165
1166 // create a new stack entry
1167 sptr = ff_create_stack_element(file_path,
1168 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
1169 if (sptr == NULL)
1170 break;
1171 ff_push(search_ctx, sptr);
1172 }
1173 else
1174 break;
1175 }
1176#endif
1177
1178fail:
1179 vim_free(file_path);
1180 return NULL;
1181}
1182
1183/*
1184 * Free the list of lists of visited files and directories
1185 * Can handle it if the passed search_context is NULL;
1186 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001187 static void
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001188vim_findfile_free_visited(void *search_ctx_arg)
1189{
1190 ff_search_ctx_T *search_ctx;
1191
1192 if (search_ctx_arg == NULL)
1193 return;
1194
1195 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
1196 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
1197 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
1198}
1199
1200 static void
1201vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp)
1202{
1203 ff_visited_list_hdr_T *vp;
1204
1205 while (*list_headp != NULL)
1206 {
1207 vp = (*list_headp)->ffvl_next;
1208 ff_free_visited_list((*list_headp)->ffvl_visited_list);
1209
1210 vim_free((*list_headp)->ffvl_filename);
1211 vim_free(*list_headp);
1212 *list_headp = vp;
1213 }
1214 *list_headp = NULL;
1215}
1216
1217 static void
1218ff_free_visited_list(ff_visited_T *vl)
1219{
1220 ff_visited_T *vp;
1221
1222 while (vl != NULL)
1223 {
1224 vp = vl->ffv_next;
1225#ifdef FEAT_PATH_EXTRA
1226 vim_free(vl->ffv_wc_path);
1227#endif
1228 vim_free(vl);
1229 vl = vp;
1230 }
1231 vl = NULL;
1232}
1233
1234/*
1235 * Returns the already visited list for the given filename. If none is found it
1236 * allocates a new one.
1237 */
1238 static ff_visited_list_hdr_T*
1239ff_get_visited_list(
1240 char_u *filename,
1241 ff_visited_list_hdr_T **list_headp)
1242{
1243 ff_visited_list_hdr_T *retptr = NULL;
1244
1245 // check if a visited list for the given filename exists
1246 if (*list_headp != NULL)
1247 {
1248 retptr = *list_headp;
1249 while (retptr != NULL)
1250 {
1251 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
1252 {
1253#ifdef FF_VERBOSE
1254 if (p_verbose >= 5)
1255 {
1256 verbose_enter_scroll();
1257 smsg("ff_get_visited_list: FOUND list for %s",
1258 filename);
1259 // don't overwrite this either
1260 msg_puts("\n");
1261 verbose_leave_scroll();
1262 }
1263#endif
1264 return retptr;
1265 }
1266 retptr = retptr->ffvl_next;
1267 }
1268 }
1269
1270#ifdef FF_VERBOSE
1271 if (p_verbose >= 5)
1272 {
1273 verbose_enter_scroll();
1274 smsg("ff_get_visited_list: new list for %s", filename);
1275 // don't overwrite this either
1276 msg_puts("\n");
1277 verbose_leave_scroll();
1278 }
1279#endif
1280
1281 /*
1282 * if we reach this we didn't find a list and we have to allocate new list
1283 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001284 retptr = ALLOC_ONE(ff_visited_list_hdr_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001285 if (retptr == NULL)
1286 return NULL;
1287
1288 retptr->ffvl_visited_list = NULL;
1289 retptr->ffvl_filename = vim_strsave(filename);
1290 if (retptr->ffvl_filename == NULL)
1291 {
1292 vim_free(retptr);
1293 return NULL;
1294 }
1295 retptr->ffvl_next = *list_headp;
1296 *list_headp = retptr;
1297
1298 return retptr;
1299}
1300
1301#ifdef FEAT_PATH_EXTRA
1302/*
1303 * check if two wildcard paths are equal. Returns TRUE or FALSE.
1304 * They are equal if:
1305 * - both paths are NULL
1306 * - they have the same length
1307 * - char by char comparison is OK
1308 * - the only differences are in the counters behind a '**', so
1309 * '**\20' is equal to '**\24'
1310 */
1311 static int
1312ff_wc_equal(char_u *s1, char_u *s2)
1313{
1314 int i, j;
1315 int c1 = NUL;
1316 int c2 = NUL;
1317 int prev1 = NUL;
1318 int prev2 = NUL;
1319
1320 if (s1 == s2)
1321 return TRUE;
1322
1323 if (s1 == NULL || s2 == NULL)
1324 return FALSE;
1325
1326 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
1327 {
1328 c1 = PTR2CHAR(s1 + i);
1329 c2 = PTR2CHAR(s2 + j);
1330
1331 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
1332 && (prev1 != '*' || prev2 != '*'))
1333 return FALSE;
1334 prev2 = prev1;
1335 prev1 = c1;
1336
Bram Moolenaar1614a142019-10-06 22:00:13 +02001337 i += mb_ptr2len(s1 + i);
1338 j += mb_ptr2len(s2 + j);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001339 }
1340 return s1[i] == s2[j];
1341}
1342#endif
1343
1344/*
1345 * maintains the list of already visited files and dirs
1346 * returns FAIL if the given file/dir is already in the list
1347 * returns OK if it is newly added
1348 *
1349 * TODO: What to do on memory allocation problems?
1350 * -> return TRUE - Better the file is found several times instead of
1351 * never.
1352 */
1353 static int
1354ff_check_visited(
1355 ff_visited_T **visited_list,
1356 char_u *fname
1357#ifdef FEAT_PATH_EXTRA
1358 , char_u *wc_path
1359#endif
1360 )
1361{
1362 ff_visited_T *vp;
1363#ifdef UNIX
1364 stat_T st;
1365 int url = FALSE;
1366#endif
1367
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001368 // For a URL we only compare the name, otherwise we compare the
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001369 // device/inode (unix) or the full path name (not Unix).
1370 if (path_with_url(fname))
1371 {
1372 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
1373#ifdef UNIX
1374 url = TRUE;
1375#endif
1376 }
1377 else
1378 {
1379 ff_expand_buffer[0] = NUL;
1380#ifdef UNIX
1381 if (mch_stat((char *)fname, &st) < 0)
1382#else
1383 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
1384#endif
1385 return FAIL;
1386 }
1387
1388 // check against list of already visited files
1389 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
1390 {
1391 if (
1392#ifdef UNIX
1393 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
1394 && vp->ffv_ino == st.st_ino)
1395 :
1396#endif
1397 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
1398 )
1399 {
1400#ifdef FEAT_PATH_EXTRA
1401 // are the wildcard parts equal
1402 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
1403#endif
1404 // already visited
1405 return FAIL;
1406 }
1407 }
1408
1409 /*
1410 * New file/dir. Add it to the list of visited files/dirs.
1411 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001412 vp = alloc(sizeof(ff_visited_T) + STRLEN(ff_expand_buffer));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001413
1414 if (vp != NULL)
1415 {
1416#ifdef UNIX
1417 if (!url)
1418 {
1419 vp->ffv_dev_valid = TRUE;
1420 vp->ffv_ino = st.st_ino;
1421 vp->ffv_dev = st.st_dev;
1422 vp->ffv_fname[0] = NUL;
1423 }
1424 else
1425 {
1426 vp->ffv_dev_valid = FALSE;
1427#endif
1428 STRCPY(vp->ffv_fname, ff_expand_buffer);
1429#ifdef UNIX
1430 }
1431#endif
1432#ifdef FEAT_PATH_EXTRA
1433 if (wc_path != NULL)
1434 vp->ffv_wc_path = vim_strsave(wc_path);
1435 else
1436 vp->ffv_wc_path = NULL;
1437#endif
1438
1439 vp->ffv_next = *visited_list;
1440 *visited_list = vp;
1441 }
1442
1443 return OK;
1444}
1445
1446/*
1447 * create stack element from given path pieces
1448 */
1449 static ff_stack_T *
1450ff_create_stack_element(
1451 char_u *fix_part,
1452#ifdef FEAT_PATH_EXTRA
1453 char_u *wc_part,
1454#endif
1455 int level,
1456 int star_star_empty)
1457{
1458 ff_stack_T *new;
1459
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001460 new = ALLOC_ONE(ff_stack_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001461 if (new == NULL)
1462 return NULL;
1463
1464 new->ffs_prev = NULL;
1465 new->ffs_filearray = NULL;
1466 new->ffs_filearray_size = 0;
1467 new->ffs_filearray_cur = 0;
1468 new->ffs_stage = 0;
1469 new->ffs_level = level;
1470 new->ffs_star_star_empty = star_star_empty;
1471
1472 // the following saves NULL pointer checks in vim_findfile
1473 if (fix_part == NULL)
1474 fix_part = (char_u *)"";
1475 new->ffs_fix_path = vim_strsave(fix_part);
1476
1477#ifdef FEAT_PATH_EXTRA
1478 if (wc_part == NULL)
1479 wc_part = (char_u *)"";
1480 new->ffs_wc_path = vim_strsave(wc_part);
1481#endif
1482
1483 if (new->ffs_fix_path == NULL
1484#ifdef FEAT_PATH_EXTRA
1485 || new->ffs_wc_path == NULL
1486#endif
1487 )
1488 {
1489 ff_free_stack_element(new);
1490 new = NULL;
1491 }
1492
1493 return new;
1494}
1495
1496/*
1497 * Push a dir on the directory stack.
1498 */
1499 static void
1500ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)
1501{
1502 // check for NULL pointer, not to return an error to the user, but
1503 // to prevent a crash
1504 if (stack_ptr != NULL)
1505 {
1506 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
1507 search_ctx->ffsc_stack_ptr = stack_ptr;
1508 }
1509}
1510
1511/*
1512 * Pop a dir from the directory stack.
1513 * Returns NULL if stack is empty.
1514 */
1515 static ff_stack_T *
1516ff_pop(ff_search_ctx_T *search_ctx)
1517{
1518 ff_stack_T *sptr;
1519
1520 sptr = search_ctx->ffsc_stack_ptr;
1521 if (search_ctx->ffsc_stack_ptr != NULL)
1522 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
1523
1524 return sptr;
1525}
1526
1527/*
1528 * free the given stack element
1529 */
1530 static void
1531ff_free_stack_element(ff_stack_T *stack_ptr)
1532{
1533 // vim_free handles possible NULL pointers
1534 vim_free(stack_ptr->ffs_fix_path);
1535#ifdef FEAT_PATH_EXTRA
1536 vim_free(stack_ptr->ffs_wc_path);
1537#endif
1538
1539 if (stack_ptr->ffs_filearray != NULL)
1540 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
1541
1542 vim_free(stack_ptr);
1543}
1544
1545/*
1546 * Clear the search context, but NOT the visited list.
1547 */
1548 static void
1549ff_clear(ff_search_ctx_T *search_ctx)
1550{
1551 ff_stack_T *sptr;
1552
1553 // clear up stack
1554 while ((sptr = ff_pop(search_ctx)) != NULL)
1555 ff_free_stack_element(sptr);
1556
1557 vim_free(search_ctx->ffsc_file_to_search);
1558 vim_free(search_ctx->ffsc_start_dir);
1559 vim_free(search_ctx->ffsc_fix_path);
1560#ifdef FEAT_PATH_EXTRA
1561 vim_free(search_ctx->ffsc_wc_path);
1562#endif
1563
1564#ifdef FEAT_PATH_EXTRA
1565 if (search_ctx->ffsc_stopdirs_v != NULL)
1566 {
1567 int i = 0;
1568
1569 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
1570 {
1571 vim_free(search_ctx->ffsc_stopdirs_v[i]);
1572 i++;
1573 }
1574 vim_free(search_ctx->ffsc_stopdirs_v);
1575 }
1576 search_ctx->ffsc_stopdirs_v = NULL;
1577#endif
1578
1579 // reset everything
1580 search_ctx->ffsc_file_to_search = NULL;
1581 search_ctx->ffsc_start_dir = NULL;
1582 search_ctx->ffsc_fix_path = NULL;
1583#ifdef FEAT_PATH_EXTRA
1584 search_ctx->ffsc_wc_path = NULL;
1585 search_ctx->ffsc_level = 0;
1586#endif
1587}
1588
1589#ifdef FEAT_PATH_EXTRA
1590/*
1591 * check if the given path is in the stopdirs
1592 * returns TRUE if yes else FALSE
1593 */
1594 static int
1595ff_path_in_stoplist(char_u *path, int path_len, char_u **stopdirs_v)
1596{
1597 int i = 0;
1598
1599 // eat up trailing path separators, except the first
1600 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
1601 path_len--;
1602
1603 // if no path consider it as match
1604 if (path_len == 0)
1605 return TRUE;
1606
1607 for (i = 0; stopdirs_v[i] != NULL; i++)
1608 {
1609 if ((int)STRLEN(stopdirs_v[i]) > path_len)
1610 {
1611 // match for parent directory. So '/home' also matches
1612 // '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
1613 // '/home/r' would also match '/home/rks'
1614 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
1615 && vim_ispathsep(stopdirs_v[i][path_len]))
1616 return TRUE;
1617 }
1618 else
1619 {
1620 if (fnamecmp(stopdirs_v[i], path) == 0)
1621 return TRUE;
1622 }
1623 }
1624 return FALSE;
1625}
1626#endif
1627
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001628/*
1629 * Find the file name "ptr[len]" in the path. Also finds directory names.
1630 *
1631 * On the first call set the parameter 'first' to TRUE to initialize
1632 * the search. For repeating calls to FALSE.
1633 *
1634 * Repeating calls will return other files called 'ptr[len]' from the path.
1635 *
1636 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
1637 * don't need valid values.
1638 *
1639 * If nothing found on the first call the option FNAME_MESS will issue the
1640 * message:
1641 * 'Can't find file "<file>" in path'
1642 * On repeating calls:
1643 * 'No more file "<file>" found in path'
1644 *
1645 * options:
1646 * FNAME_MESS give error message when not found
1647 *
1648 * Uses NameBuff[]!
1649 *
1650 * Returns an allocated string for the file name. NULL for error.
1651 *
1652 */
1653 char_u *
1654find_file_in_path(
1655 char_u *ptr, // file name
1656 int len, // length of file name
1657 int options,
1658 int first, // use count'th matching file name
1659 char_u *rel_fname) // file name searching relative to
1660{
1661 return find_file_in_path_option(ptr, len, options, first,
1662 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
1663 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
1664}
1665
1666static char_u *ff_file_to_find = NULL;
1667static void *fdip_search_ctx = NULL;
1668
1669# if defined(EXITFREE) || defined(PROTO)
1670 void
1671free_findfile(void)
1672{
1673 vim_free(ff_file_to_find);
1674 vim_findfile_cleanup(fdip_search_ctx);
1675 vim_free(ff_expand_buffer);
1676}
1677# endif
1678
1679/*
1680 * Find the directory name "ptr[len]" in the path.
1681 *
1682 * options:
1683 * FNAME_MESS give error message when not found
1684 * FNAME_UNESC unescape backslashes.
1685 *
1686 * Uses NameBuff[]!
1687 *
1688 * Returns an allocated string for the file name. NULL for error.
1689 */
1690 char_u *
1691find_directory_in_path(
1692 char_u *ptr, // file name
1693 int len, // length of file name
1694 int options,
1695 char_u *rel_fname) // file name searching relative to
1696{
1697 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
1698 FINDFILE_DIR, rel_fname, (char_u *)"");
1699}
1700
1701 char_u *
1702find_file_in_path_option(
1703 char_u *ptr, // file name
1704 int len, // length of file name
1705 int options,
1706 int first, // use count'th matching file name
1707 char_u *path_option, // p_path or p_cdpath
1708 int find_what, // FINDFILE_FILE, _DIR or _BOTH
1709 char_u *rel_fname, // file name we are looking relative to.
1710 char_u *suffixes) // list of suffixes, 'suffixesadd' option
1711{
1712 static char_u *dir;
1713 static int did_findfile_init = FALSE;
1714 char_u save_char;
1715 char_u *file_name = NULL;
1716 char_u *buf = NULL;
1717 int rel_to_curdir;
1718# ifdef AMIGA
1719 struct Process *proc = (struct Process *)FindTask(0L);
1720 APTR save_winptr = proc->pr_WindowPtr;
1721
1722 // Avoid a requester here for a volume that doesn't exist.
1723 proc->pr_WindowPtr = (APTR)-1L;
1724# endif
1725
1726 if (first == TRUE)
1727 {
Bram Moolenaare015d992021-11-17 19:01:53 +00001728 if (len == 0)
1729 return NULL;
1730
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001731 // copy file name into NameBuff, expanding environment variables
1732 save_char = ptr[len];
1733 ptr[len] = NUL;
1734 expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
1735 ptr[len] = save_char;
1736
1737 vim_free(ff_file_to_find);
1738 ff_file_to_find = vim_strsave(NameBuff);
1739 if (ff_file_to_find == NULL) // out of memory
1740 {
1741 file_name = NULL;
1742 goto theend;
1743 }
1744 if (options & FNAME_UNESC)
1745 {
1746 // Change all "\ " to " ".
1747 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
1748 if (ptr[0] == '\\' && ptr[1] == ' ')
1749 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
1750 }
1751 }
1752
1753 rel_to_curdir = (ff_file_to_find[0] == '.'
1754 && (ff_file_to_find[1] == NUL
1755 || vim_ispathsep(ff_file_to_find[1])
1756 || (ff_file_to_find[1] == '.'
1757 && (ff_file_to_find[2] == NUL
1758 || vim_ispathsep(ff_file_to_find[2])))));
1759 if (vim_isAbsName(ff_file_to_find)
1760 // "..", "../path", "." and "./path": don't use the path_option
1761 || rel_to_curdir
1762# if defined(MSWIN)
1763 // handle "\tmp" as absolute path
1764 || vim_ispathsep(ff_file_to_find[0])
1765 // handle "c:name" as absolute path
1766 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
1767# endif
1768# ifdef AMIGA
1769 // handle ":tmp" as absolute path
1770 || ff_file_to_find[0] == ':'
1771# endif
1772 )
1773 {
1774 /*
1775 * Absolute path, no need to use "path_option".
1776 * If this is not a first call, return NULL. We already returned a
1777 * filename on the first call.
1778 */
1779 if (first == TRUE)
1780 {
1781 int l;
1782 int run;
1783
1784 if (path_with_url(ff_file_to_find))
1785 {
1786 file_name = vim_strsave(ff_file_to_find);
1787 goto theend;
1788 }
1789
1790 // When FNAME_REL flag given first use the directory of the file.
1791 // Otherwise or when this fails use the current directory.
1792 for (run = 1; run <= 2; ++run)
1793 {
1794 l = (int)STRLEN(ff_file_to_find);
1795 if (run == 1
1796 && rel_to_curdir
1797 && (options & FNAME_REL)
1798 && rel_fname != NULL
1799 && STRLEN(rel_fname) + l < MAXPATHL)
1800 {
1801 STRCPY(NameBuff, rel_fname);
1802 STRCPY(gettail(NameBuff), ff_file_to_find);
1803 l = (int)STRLEN(NameBuff);
1804 }
1805 else
1806 {
1807 STRCPY(NameBuff, ff_file_to_find);
1808 run = 2;
1809 }
1810
1811 // When the file doesn't exist, try adding parts of
1812 // 'suffixesadd'.
1813 buf = suffixes;
1814 for (;;)
1815 {
1816 if (mch_getperm(NameBuff) >= 0
1817 && (find_what == FINDFILE_BOTH
1818 || ((find_what == FINDFILE_DIR)
1819 == mch_isdir(NameBuff))))
1820 {
1821 file_name = vim_strsave(NameBuff);
1822 goto theend;
1823 }
1824 if (*buf == NUL)
1825 break;
1826 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
1827 }
1828 }
1829 }
1830 }
1831 else
1832 {
1833 /*
1834 * Loop over all paths in the 'path' or 'cdpath' option.
1835 * When "first" is set, first setup to the start of the option.
1836 * Otherwise continue to find the next match.
1837 */
1838 if (first == TRUE)
1839 {
1840 // vim_findfile_free_visited can handle a possible NULL pointer
1841 vim_findfile_free_visited(fdip_search_ctx);
1842 dir = path_option;
1843 did_findfile_init = FALSE;
1844 }
1845
1846 for (;;)
1847 {
1848 if (did_findfile_init)
1849 {
1850 file_name = vim_findfile(fdip_search_ctx);
1851 if (file_name != NULL)
1852 break;
1853
1854 did_findfile_init = FALSE;
1855 }
1856 else
1857 {
1858 char_u *r_ptr;
1859
1860 if (dir == NULL || *dir == NUL)
1861 {
1862 // We searched all paths of the option, now we can
1863 // free the search context.
1864 vim_findfile_cleanup(fdip_search_ctx);
1865 fdip_search_ctx = NULL;
1866 break;
1867 }
1868
Bram Moolenaar51e14382019-05-25 20:21:28 +02001869 if ((buf = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001870 break;
1871
1872 // copy next path
1873 buf[0] = 0;
1874 copy_option_part(&dir, buf, MAXPATHL, " ,");
1875
1876# ifdef FEAT_PATH_EXTRA
1877 // get the stopdir string
1878 r_ptr = vim_findfile_stopdir(buf);
1879# else
1880 r_ptr = NULL;
1881# endif
1882 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
1883 r_ptr, 100, FALSE, find_what,
1884 fdip_search_ctx, FALSE, rel_fname);
1885 if (fdip_search_ctx != NULL)
1886 did_findfile_init = TRUE;
1887 vim_free(buf);
1888 }
1889 }
1890 }
1891 if (file_name == NULL && (options & FNAME_MESS))
1892 {
1893 if (first == TRUE)
1894 {
1895 if (find_what == FINDFILE_DIR)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001896 semsg(_(e_cant_find_directory_str_in_cdpath),
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001897 ff_file_to_find);
1898 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001899 semsg(_(e_cant_find_file_str_in_path),
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001900 ff_file_to_find);
1901 }
1902 else
1903 {
1904 if (find_what == FINDFILE_DIR)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001905 semsg(_(e_no_more_directory_str_found_in_cdpath),
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001906 ff_file_to_find);
1907 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001908 semsg(_(e_no_more_file_str_found_in_path),
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001909 ff_file_to_find);
1910 }
1911 }
1912
1913theend:
1914# ifdef AMIGA
1915 proc->pr_WindowPtr = save_winptr;
1916# endif
1917 return file_name;
1918}
1919
1920/*
1921 * Get the file name at the cursor.
1922 * If Visual mode is active, use the selected text if it's in one line.
1923 * Returns the name in allocated memory, NULL for failure.
1924 */
1925 char_u *
1926grab_file_name(long count, linenr_T *file_lnum)
1927{
1928 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
1929
1930 if (VIsual_active)
1931 {
1932 int len;
1933 char_u *ptr;
1934
1935 if (get_visual_text(NULL, &ptr, &len) == FAIL)
1936 return NULL;
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +02001937 // Only recognize ":123" here
1938 if (file_lnum != NULL && ptr[len] == ':' && isdigit(ptr[len + 1]))
1939 {
1940 char_u *p = ptr + len + 1;
1941
1942 *file_lnum = getdigits(&p);
1943 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001944 return find_file_name_in_path(ptr, len, options,
1945 count, curbuf->b_ffname);
1946 }
1947 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
1948}
1949
1950/*
1951 * Return the file name under or after the cursor.
1952 *
1953 * The 'path' option is searched if the file name is not absolute.
1954 * The string returned has been alloc'ed and should be freed by the caller.
1955 * NULL is returned if the file name or file is not found.
1956 *
1957 * options:
1958 * FNAME_MESS give error messages
1959 * FNAME_EXP expand to path
1960 * FNAME_HYP check for hypertext link
1961 * FNAME_INCL apply "includeexpr"
1962 */
1963 char_u *
1964file_name_at_cursor(int options, long count, linenr_T *file_lnum)
1965{
1966 return file_name_in_line(ml_get_curline(),
1967 curwin->w_cursor.col, options, count, curbuf->b_ffname,
1968 file_lnum);
1969}
1970
1971/*
1972 * Return the name of the file under or after ptr[col].
1973 * Otherwise like file_name_at_cursor().
1974 */
1975 char_u *
1976file_name_in_line(
1977 char_u *line,
1978 int col,
1979 int options,
1980 long count,
1981 char_u *rel_fname, // file we are searching relative to
1982 linenr_T *file_lnum) // line number after the file name
1983{
1984 char_u *ptr;
1985 int len;
1986 int in_type = TRUE;
1987 int is_url = FALSE;
1988
1989 /*
1990 * search forward for what could be the start of a file name
1991 */
1992 ptr = line + col;
1993 while (*ptr != NUL && !vim_isfilec(*ptr))
1994 MB_PTR_ADV(ptr);
1995 if (*ptr == NUL) // nothing found
1996 {
1997 if (options & FNAME_MESS)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001998 emsg(_(e_no_file_name_under_cursor));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001999 return NULL;
2000 }
2001
2002 /*
2003 * Search backward for first char of the file name.
2004 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
2005 */
2006 while (ptr > line)
2007 {
2008 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
2009 ptr -= len + 1;
2010 else if (vim_isfilec(ptr[-1])
2011 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
2012 --ptr;
2013 else
2014 break;
2015 }
2016
2017 /*
2018 * Search forward for the last char of the file name.
2019 * Also allow "://" when ':' is not in 'isfname'.
2020 */
2021 len = 0;
2022 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
2023 || ((options & FNAME_HYP) && path_is_url(ptr + len))
Bram Moolenaarcbef8e12019-03-09 12:32:56 +01002024 || (is_url && vim_strchr((char_u *)":?&=", ptr[len]) != NULL))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002025 {
Bram Moolenaarcbef8e12019-03-09 12:32:56 +01002026 // After type:// we also include :, ?, & and = as valid characters, so that
2027 // http://google.com:8080?q=this&that=ok works.
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002028 if ((ptr[len] >= 'A' && ptr[len] <= 'Z') || (ptr[len] >= 'a' && ptr[len] <= 'z'))
2029 {
2030 if (in_type && path_is_url(ptr + len + 1))
2031 is_url = TRUE;
2032 }
2033 else
2034 in_type = FALSE;
2035
2036 if (ptr[len] == '\\')
2037 // Skip over the "\" in "\ ".
2038 ++len;
2039 if (has_mbyte)
2040 len += (*mb_ptr2len)(ptr + len);
2041 else
2042 ++len;
2043 }
2044
2045 /*
2046 * If there is trailing punctuation, remove it.
2047 * But don't remove "..", could be a directory name.
2048 */
2049 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
2050 && ptr[len - 2] != '.')
2051 --len;
2052
2053 if (file_lnum != NULL)
2054 {
2055 char_u *p;
Bram Moolenaar64e74c92019-12-22 15:38:06 +01002056 char *line_english = " line ";
2057 char *line_transl = _(line_msg);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002058
Bram Moolenaar64e74c92019-12-22 15:38:06 +01002059 // Get the number after the file name and a separator character.
2060 // Also accept " line 999" with and without the same translation as
2061 // used in last_set_msg().
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002062 p = ptr + len;
Bram Moolenaar64e74c92019-12-22 15:38:06 +01002063 if (STRNCMP(p, line_english, STRLEN(line_english)) == 0)
2064 p += STRLEN(line_english);
2065 else if (STRNCMP(p, line_transl, STRLEN(line_transl)) == 0)
2066 p += STRLEN(line_transl);
2067 else
2068 p = skipwhite(p);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002069 if (*p != NUL)
2070 {
2071 if (!isdigit(*p))
2072 ++p; // skip the separator
2073 p = skipwhite(p);
2074 if (isdigit(*p))
2075 *file_lnum = (int)getdigits(&p);
2076 }
2077 }
2078
2079 return find_file_name_in_path(ptr, len, options, count, rel_fname);
2080}
2081
2082# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2083 static char_u *
2084eval_includeexpr(char_u *ptr, int len)
2085{
2086 char_u *res;
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002087 sctx_T save_sctx = current_sctx;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002088
2089 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002090 current_sctx = curbuf->b_p_script_ctx[BV_INEX];
2091
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002092 res = eval_to_string_safe(curbuf->b_p_inex,
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002093 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL), TRUE);
2094
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002095 set_vim_var_string(VV_FNAME, NULL, 0);
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002096 current_sctx = save_sctx;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002097 return res;
2098}
2099# endif
2100
2101/*
2102 * Return the name of the file ptr[len] in 'path'.
2103 * Otherwise like file_name_at_cursor().
2104 */
2105 char_u *
2106find_file_name_in_path(
2107 char_u *ptr,
2108 int len,
2109 int options,
2110 long count,
2111 char_u *rel_fname) // file we are searching relative to
2112{
2113 char_u *file_name;
2114 int c;
2115# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2116 char_u *tofree = NULL;
Bram Moolenaar615ddd52021-11-17 18:00:31 +00002117# endif
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002118
Bram Moolenaar615ddd52021-11-17 18:00:31 +00002119 if (len == 0)
2120 return NULL;
2121
2122# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002123 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
2124 {
2125 tofree = eval_includeexpr(ptr, len);
2126 if (tofree != NULL)
2127 {
2128 ptr = tofree;
2129 len = (int)STRLEN(ptr);
2130 }
2131 }
2132# endif
2133
2134 if (options & FNAME_EXP)
2135 {
2136 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
2137 TRUE, rel_fname);
2138
2139# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2140 /*
2141 * If the file could not be found in a normal way, try applying
2142 * 'includeexpr' (unless done already).
2143 */
2144 if (file_name == NULL
2145 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
2146 {
2147 tofree = eval_includeexpr(ptr, len);
2148 if (tofree != NULL)
2149 {
2150 ptr = tofree;
2151 len = (int)STRLEN(ptr);
2152 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
2153 TRUE, rel_fname);
2154 }
2155 }
2156# endif
2157 if (file_name == NULL && (options & FNAME_MESS))
2158 {
2159 c = ptr[len];
2160 ptr[len] = NUL;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002161 semsg(_(e_cant_find_file_str_in_path_2), ptr);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002162 ptr[len] = c;
2163 }
2164
2165 // Repeat finding the file "count" times. This matters when it
2166 // appears several times in the path.
2167 while (file_name != NULL && --count > 0)
2168 {
2169 vim_free(file_name);
2170 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
2171 }
2172 }
2173 else
2174 file_name = vim_strnsave(ptr, len);
2175
2176# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2177 vim_free(tofree);
2178# endif
2179
2180 return file_name;
2181}
2182
2183/*
2184 * Return the end of the directory name, on the first path
2185 * separator:
2186 * "/path/file", "/path/dir/", "/path//dir", "/file"
2187 * ^ ^ ^ ^
2188 */
2189 static char_u *
2190gettail_dir(char_u *fname)
2191{
2192 char_u *dir_end = fname;
2193 char_u *next_dir_end = fname;
2194 int look_for_sep = TRUE;
2195 char_u *p;
2196
2197 for (p = fname; *p != NUL; )
2198 {
2199 if (vim_ispathsep(*p))
2200 {
2201 if (look_for_sep)
2202 {
2203 next_dir_end = p;
2204 look_for_sep = FALSE;
2205 }
2206 }
2207 else
2208 {
2209 if (!look_for_sep)
2210 dir_end = next_dir_end;
2211 look_for_sep = TRUE;
2212 }
2213 MB_PTR_ADV(p);
2214 }
2215 return dir_end;
2216}
2217
2218/*
2219 * return TRUE if 'c' is a path list separator.
2220 */
2221 int
2222vim_ispathlistsep(int c)
2223{
2224# ifdef UNIX
2225 return (c == ':');
2226# else
2227 return (c == ';'); // might not be right for every system...
2228# endif
2229}
2230
2231/*
2232 * Moves "*psep" back to the previous path separator in "path".
2233 * Returns FAIL is "*psep" ends up at the beginning of "path".
2234 */
2235 static int
2236find_previous_pathsep(char_u *path, char_u **psep)
2237{
2238 // skip the current separator
2239 if (*psep > path && vim_ispathsep(**psep))
2240 --*psep;
2241
2242 // find the previous separator
2243 while (*psep > path)
2244 {
2245 if (vim_ispathsep(**psep))
2246 return OK;
2247 MB_PTR_BACK(path, *psep);
2248 }
2249
2250 return FAIL;
2251}
2252
2253/*
2254 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
2255 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
2256 */
2257 static int
2258is_unique(char_u *maybe_unique, garray_T *gap, int i)
2259{
2260 int j;
2261 int candidate_len;
2262 int other_path_len;
2263 char_u **other_paths = (char_u **)gap->ga_data;
2264 char_u *rival;
2265
2266 for (j = 0; j < gap->ga_len; j++)
2267 {
2268 if (j == i)
2269 continue; // don't compare it with itself
2270
2271 candidate_len = (int)STRLEN(maybe_unique);
2272 other_path_len = (int)STRLEN(other_paths[j]);
2273 if (other_path_len < candidate_len)
2274 continue; // it's different when it's shorter
2275
2276 rival = other_paths[j] + other_path_len - candidate_len;
2277 if (fnamecmp(maybe_unique, rival) == 0
2278 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
2279 return FALSE; // match
2280 }
2281
2282 return TRUE; // no match found
2283}
2284
2285/*
2286 * Split the 'path' option into an array of strings in garray_T. Relative
2287 * paths are expanded to their equivalent fullpath. This includes the "."
2288 * (relative to current buffer directory) and empty path (relative to current
2289 * directory) notations.
2290 *
2291 * TODO: handle upward search (;) and path limiter (**N) notations by
2292 * expanding each into their equivalent path(s).
2293 */
2294 static void
2295expand_path_option(char_u *curdir, garray_T *gap)
2296{
2297 char_u *path_option = *curbuf->b_p_path == NUL
2298 ? p_path : curbuf->b_p_path;
2299 char_u *buf;
2300 char_u *p;
2301 int len;
2302
Bram Moolenaar51e14382019-05-25 20:21:28 +02002303 if ((buf = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002304 return;
2305
2306 while (*path_option != NUL)
2307 {
2308 copy_option_part(&path_option, buf, MAXPATHL, " ,");
2309
2310 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
2311 {
2312 // Relative to current buffer:
2313 // "/path/file" + "." -> "/path/"
2314 // "/path/file" + "./subdir" -> "/path/subdir"
2315 if (curbuf->b_ffname == NULL)
2316 continue;
2317 p = gettail(curbuf->b_ffname);
2318 len = (int)(p - curbuf->b_ffname);
2319 if (len + (int)STRLEN(buf) >= MAXPATHL)
2320 continue;
2321 if (buf[1] == NUL)
2322 buf[len] = NUL;
2323 else
2324 STRMOVE(buf + len, buf + 2);
2325 mch_memmove(buf, curbuf->b_ffname, len);
2326 simplify_filename(buf);
2327 }
2328 else if (buf[0] == NUL)
2329 // relative to current directory
2330 STRCPY(buf, curdir);
2331 else if (path_with_url(buf))
2332 // URL can't be used here
2333 continue;
2334 else if (!mch_isFullName(buf))
2335 {
2336 // Expand relative path to their full path equivalent
2337 len = (int)STRLEN(curdir);
2338 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
2339 continue;
2340 STRMOVE(buf + len + 1, buf);
2341 STRCPY(buf, curdir);
2342 buf[len] = PATHSEP;
2343 simplify_filename(buf);
2344 }
2345
2346 if (ga_grow(gap, 1) == FAIL)
2347 break;
2348
2349# if defined(MSWIN)
2350 // Avoid the path ending in a backslash, it fails when a comma is
2351 // appended.
2352 len = (int)STRLEN(buf);
2353 if (buf[len - 1] == '\\')
2354 buf[len - 1] = '/';
2355# endif
2356
2357 p = vim_strsave(buf);
2358 if (p == NULL)
2359 break;
2360 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
2361 }
2362
2363 vim_free(buf);
2364}
2365
2366/*
2367 * Returns a pointer to the file or directory name in "fname" that matches the
2368 * longest path in "ga"p, or NULL if there is no match. For example:
2369 *
2370 * path: /foo/bar/baz
2371 * fname: /foo/bar/baz/quux.txt
2372 * returns: ^this
2373 */
2374 static char_u *
2375get_path_cutoff(char_u *fname, garray_T *gap)
2376{
2377 int i;
2378 int maxlen = 0;
2379 char_u **path_part = (char_u **)gap->ga_data;
2380 char_u *cutoff = NULL;
2381
2382 for (i = 0; i < gap->ga_len; i++)
2383 {
2384 int j = 0;
2385
2386 while ((fname[j] == path_part[i][j]
2387# if defined(MSWIN)
2388 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
2389# endif
2390 ) && fname[j] != NUL && path_part[i][j] != NUL)
2391 j++;
2392 if (j > maxlen)
2393 {
2394 maxlen = j;
2395 cutoff = &fname[j];
2396 }
2397 }
2398
2399 // skip to the file or directory name
2400 if (cutoff != NULL)
2401 while (vim_ispathsep(*cutoff))
2402 MB_PTR_ADV(cutoff);
2403
2404 return cutoff;
2405}
2406
2407/*
2408 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
2409 * that they are unique with respect to each other while conserving the part
2410 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
2411 */
2412 void
2413uniquefy_paths(garray_T *gap, char_u *pattern)
2414{
2415 int i;
2416 int len;
2417 char_u **fnames = (char_u **)gap->ga_data;
2418 int sort_again = FALSE;
2419 char_u *pat;
2420 char_u *file_pattern;
2421 char_u *curdir;
2422 regmatch_T regmatch;
2423 garray_T path_ga;
2424 char_u **in_curdir = NULL;
2425 char_u *short_name;
2426
2427 remove_duplicates(gap);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002428 ga_init2(&path_ga, sizeof(char_u *), 1);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002429
2430 /*
2431 * We need to prepend a '*' at the beginning of file_pattern so that the
2432 * regex matches anywhere in the path. FIXME: is this valid for all
2433 * possible patterns?
2434 */
2435 len = (int)STRLEN(pattern);
2436 file_pattern = alloc(len + 2);
2437 if (file_pattern == NULL)
2438 return;
2439 file_pattern[0] = '*';
2440 file_pattern[1] = NUL;
2441 STRCAT(file_pattern, pattern);
2442 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
2443 vim_free(file_pattern);
2444 if (pat == NULL)
2445 return;
2446
2447 regmatch.rm_ic = TRUE; // always ignore case
2448 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2449 vim_free(pat);
2450 if (regmatch.regprog == NULL)
2451 return;
2452
Bram Moolenaar51e14382019-05-25 20:21:28 +02002453 if ((curdir = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002454 goto theend;
2455 mch_dirname(curdir, MAXPATHL);
2456 expand_path_option(curdir, &path_ga);
2457
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002458 in_curdir = ALLOC_CLEAR_MULT(char_u *, gap->ga_len);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002459 if (in_curdir == NULL)
2460 goto theend;
2461
2462 for (i = 0; i < gap->ga_len && !got_int; i++)
2463 {
2464 char_u *path = fnames[i];
2465 int is_in_curdir;
2466 char_u *dir_end = gettail_dir(path);
2467 char_u *pathsep_p;
2468 char_u *path_cutoff;
2469
2470 len = (int)STRLEN(path);
2471 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
2472 && curdir[dir_end - path] == NUL;
2473 if (is_in_curdir)
2474 in_curdir[i] = vim_strsave(path);
2475
2476 // Shorten the filename while maintaining its uniqueness
2477 path_cutoff = get_path_cutoff(path, &path_ga);
2478
2479 // Don't assume all files can be reached without path when search
2480 // pattern starts with star star slash, so only remove path_cutoff
2481 // when possible.
2482 if (pattern[0] == '*' && pattern[1] == '*'
2483 && vim_ispathsep_nocolon(pattern[2])
2484 && path_cutoff != NULL
2485 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
2486 && is_unique(path_cutoff, gap, i))
2487 {
2488 sort_again = TRUE;
2489 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
2490 }
2491 else
2492 {
2493 // Here all files can be reached without path, so get shortest
2494 // unique path. We start at the end of the path.
2495 pathsep_p = path + len - 1;
2496
2497 while (find_previous_pathsep(path, &pathsep_p))
2498 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
2499 && is_unique(pathsep_p + 1, gap, i)
2500 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
2501 {
2502 sort_again = TRUE;
2503 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
2504 break;
2505 }
2506 }
2507
2508 if (mch_isFullName(path))
2509 {
2510 /*
2511 * Last resort: shorten relative to curdir if possible.
2512 * 'possible' means:
2513 * 1. It is under the current directory.
2514 * 2. The result is actually shorter than the original.
2515 *
2516 * Before curdir After
2517 * /foo/bar/file.txt /foo/bar ./file.txt
2518 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
2519 * /file.txt / /file.txt
2520 * c:\file.txt c:\ .\file.txt
2521 */
2522 short_name = shorten_fname(path, curdir);
2523 if (short_name != NULL && short_name > path + 1
2524# if defined(MSWIN)
2525 // On windows,
2526 // shorten_fname("c:\a\a.txt", "c:\a\b")
2527 // returns "\a\a.txt", which is not really the short
2528 // name, hence:
2529 && !vim_ispathsep(*short_name)
2530# endif
2531 )
2532 {
2533 STRCPY(path, ".");
2534 add_pathsep(path);
2535 STRMOVE(path + STRLEN(path), short_name);
2536 }
2537 }
2538 ui_breakcheck();
2539 }
2540
2541 // Shorten filenames in /in/current/directory/{filename}
2542 for (i = 0; i < gap->ga_len && !got_int; i++)
2543 {
2544 char_u *rel_path;
2545 char_u *path = in_curdir[i];
2546
2547 if (path == NULL)
2548 continue;
2549
2550 // If the {filename} is not unique, change it to ./{filename}.
2551 // Else reduce it to {filename}
2552 short_name = shorten_fname(path, curdir);
2553 if (short_name == NULL)
2554 short_name = path;
2555 if (is_unique(short_name, gap, i))
2556 {
2557 STRCPY(fnames[i], short_name);
2558 continue;
2559 }
2560
Bram Moolenaar51e14382019-05-25 20:21:28 +02002561 rel_path = alloc(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002562 if (rel_path == NULL)
2563 goto theend;
2564 STRCPY(rel_path, ".");
2565 add_pathsep(rel_path);
2566 STRCAT(rel_path, short_name);
2567
2568 vim_free(fnames[i]);
2569 fnames[i] = rel_path;
2570 sort_again = TRUE;
2571 ui_breakcheck();
2572 }
2573
2574theend:
2575 vim_free(curdir);
2576 if (in_curdir != NULL)
2577 {
2578 for (i = 0; i < gap->ga_len; i++)
2579 vim_free(in_curdir[i]);
2580 vim_free(in_curdir);
2581 }
2582 ga_clear_strings(&path_ga);
2583 vim_regfree(regmatch.regprog);
2584
2585 if (sort_again)
2586 remove_duplicates(gap);
2587}
2588
2589/*
2590 * Calls globpath() with 'path' values for the given pattern and stores the
2591 * result in "gap".
2592 * Returns the total number of matches.
2593 */
2594 int
2595expand_in_path(
2596 garray_T *gap,
2597 char_u *pattern,
2598 int flags) // EW_* flags
2599{
2600 char_u *curdir;
2601 garray_T path_ga;
2602 char_u *paths = NULL;
2603 int glob_flags = 0;
2604
Bram Moolenaar964b3742019-05-24 18:54:09 +02002605 if ((curdir = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002606 return 0;
2607 mch_dirname(curdir, MAXPATHL);
2608
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002609 ga_init2(&path_ga, sizeof(char_u *), 1);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002610 expand_path_option(curdir, &path_ga);
2611 vim_free(curdir);
2612 if (path_ga.ga_len == 0)
2613 return 0;
2614
2615 paths = ga_concat_strings(&path_ga, ",");
2616 ga_clear_strings(&path_ga);
2617 if (paths == NULL)
2618 return 0;
2619
2620 if (flags & EW_ICASE)
2621 glob_flags |= WILD_ICASE;
2622 if (flags & EW_ADDSLASH)
2623 glob_flags |= WILD_ADD_SLASH;
2624 globpath(paths, pattern, gap, glob_flags);
2625 vim_free(paths);
2626
2627 return gap->ga_len;
2628}
2629
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002630
2631/*
2632 * Converts a file name into a canonical form. It simplifies a file name into
2633 * its simplest form by stripping out unneeded components, if any. The
2634 * resulting file name is simplified in place and will either be the same
2635 * length as that supplied, or shorter.
2636 */
2637 void
2638simplify_filename(char_u *filename)
2639{
2640#ifndef AMIGA // Amiga doesn't have "..", it uses "/"
2641 int components = 0;
2642 char_u *p, *tail, *start;
2643 int stripping_disabled = FALSE;
2644 int relative = TRUE;
2645
2646 p = filename;
2647# ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmanan6df0f272021-12-16 13:06:10 +00002648 if (p[0] != NUL && p[1] == ':') // skip "x:"
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002649 p += 2;
2650# endif
2651
2652 if (vim_ispathsep(*p))
2653 {
2654 relative = FALSE;
2655 do
2656 ++p;
2657 while (vim_ispathsep(*p));
2658 }
2659 start = p; // remember start after "c:/" or "/" or "///"
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02002660#ifdef UNIX
2661 // Posix says that "//path" is unchanged but "///path" is "/path".
2662 if (start > filename + 2)
2663 {
2664 STRMOVE(filename + 1, p);
2665 start = p = filename + 1;
2666 }
2667#endif
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002668
2669 do
2670 {
2671 // At this point "p" is pointing to the char following a single "/"
2672 // or "p" is at the "start" of the (absolute or relative) path name.
2673# ifdef VMS
2674 // VMS allows device:[path] - don't strip the [ in directory
2675 if ((*p == '[' || *p == '<') && p > filename && p[-1] == ':')
2676 {
2677 // :[ or :< composition: vms directory component
2678 ++components;
2679 p = getnextcomp(p + 1);
2680 }
2681 // allow remote calls as host"user passwd"::device:[path]
2682 else if (p[0] == ':' && p[1] == ':' && p > filename && p[-1] == '"' )
2683 {
2684 // ":: composition: vms host/passwd component
2685 ++components;
2686 p = getnextcomp(p + 2);
2687 }
2688 else
2689# endif
2690 if (vim_ispathsep(*p))
2691 STRMOVE(p, p + 1); // remove duplicate "/"
2692 else if (p[0] == '.' && (vim_ispathsep(p[1]) || p[1] == NUL))
2693 {
2694 if (p == start && relative)
2695 p += 1 + (p[1] != NUL); // keep single "." or leading "./"
2696 else
2697 {
2698 // Strip "./" or ".///". If we are at the end of the file name
2699 // and there is no trailing path separator, either strip "/." if
2700 // we are after "start", or strip "." if we are at the beginning
2701 // of an absolute path name .
2702 tail = p + 1;
2703 if (p[1] != NUL)
2704 while (vim_ispathsep(*tail))
2705 MB_PTR_ADV(tail);
2706 else if (p > start)
2707 --p; // strip preceding path separator
2708 STRMOVE(p, tail);
2709 }
2710 }
2711 else if (p[0] == '.' && p[1] == '.' &&
2712 (vim_ispathsep(p[2]) || p[2] == NUL))
2713 {
2714 // Skip to after ".." or "../" or "..///".
2715 tail = p + 2;
2716 while (vim_ispathsep(*tail))
2717 MB_PTR_ADV(tail);
2718
2719 if (components > 0) // strip one preceding component
2720 {
2721 int do_strip = FALSE;
2722 char_u saved_char;
2723 stat_T st;
2724
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002725 // Don't strip for an erroneous file name.
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002726 if (!stripping_disabled)
2727 {
2728 // If the preceding component does not exist in the file
2729 // system, we strip it. On Unix, we don't accept a symbolic
2730 // link that refers to a non-existent file.
2731 saved_char = p[-1];
2732 p[-1] = NUL;
2733# ifdef UNIX
2734 if (mch_lstat((char *)filename, &st) < 0)
2735# else
2736 if (mch_stat((char *)filename, &st) < 0)
2737# endif
2738 do_strip = TRUE;
2739 p[-1] = saved_char;
2740
2741 --p;
2742 // Skip back to after previous '/'.
2743 while (p > start && !after_pathsep(start, p))
2744 MB_PTR_BACK(start, p);
2745
2746 if (!do_strip)
2747 {
2748 // If the component exists in the file system, check
2749 // that stripping it won't change the meaning of the
2750 // file name. First get information about the
2751 // unstripped file name. This may fail if the component
2752 // to strip is not a searchable directory (but a regular
2753 // file, for instance), since the trailing "/.." cannot
2754 // be applied then. We don't strip it then since we
2755 // don't want to replace an erroneous file name by
2756 // a valid one, and we disable stripping of later
2757 // components.
2758 saved_char = *tail;
2759 *tail = NUL;
2760 if (mch_stat((char *)filename, &st) >= 0)
2761 do_strip = TRUE;
2762 else
2763 stripping_disabled = TRUE;
2764 *tail = saved_char;
2765# ifdef UNIX
2766 if (do_strip)
2767 {
2768 stat_T new_st;
2769
2770 // On Unix, the check for the unstripped file name
2771 // above works also for a symbolic link pointing to
2772 // a searchable directory. But then the parent of
2773 // the directory pointed to by the link must be the
2774 // same as the stripped file name. (The latter
2775 // exists in the file system since it is the
2776 // component's parent directory.)
2777 if (p == start && relative)
2778 (void)mch_stat(".", &new_st);
2779 else
2780 {
2781 saved_char = *p;
2782 *p = NUL;
2783 (void)mch_stat((char *)filename, &new_st);
2784 *p = saved_char;
2785 }
2786
2787 if (new_st.st_ino != st.st_ino ||
2788 new_st.st_dev != st.st_dev)
2789 {
2790 do_strip = FALSE;
2791 // We don't disable stripping of later
2792 // components since the unstripped path name is
2793 // still valid.
2794 }
2795 }
2796# endif
2797 }
2798 }
2799
2800 if (!do_strip)
2801 {
2802 // Skip the ".." or "../" and reset the counter for the
2803 // components that might be stripped later on.
2804 p = tail;
2805 components = 0;
2806 }
2807 else
2808 {
2809 // Strip previous component. If the result would get empty
2810 // and there is no trailing path separator, leave a single
2811 // "." instead. If we are at the end of the file name and
2812 // there is no trailing path separator and a preceding
2813 // component is left after stripping, strip its trailing
2814 // path separator as well.
2815 if (p == start && relative && tail[-1] == '.')
2816 {
2817 *p++ = '.';
2818 *p = NUL;
2819 }
2820 else
2821 {
2822 if (p > start && tail[-1] == '.')
2823 --p;
2824 STRMOVE(p, tail); // strip previous component
2825 }
2826
2827 --components;
2828 }
2829 }
2830 else if (p == start && !relative) // leading "/.." or "/../"
2831 STRMOVE(p, tail); // strip ".." or "../"
2832 else
2833 {
2834 if (p == start + 2 && p[-2] == '.') // leading "./../"
2835 {
2836 STRMOVE(p - 2, p); // strip leading "./"
2837 tail -= 2;
2838 }
2839 p = tail; // skip to char after ".." or "../"
2840 }
2841 }
2842 else
2843 {
2844 ++components; // simple path component
2845 p = getnextcomp(p);
2846 }
2847 } while (*p != NUL);
2848#endif // !AMIGA
2849}
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002850
2851#if defined(FEAT_EVAL) || defined(PROTO)
2852/*
2853 * "simplify()" function
2854 */
2855 void
2856f_simplify(typval_T *argvars, typval_T *rettv)
2857{
2858 char_u *p;
2859
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002860 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2861 return;
2862
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02002863 p = tv_get_string_strict(&argvars[0]);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002864 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002865 simplify_filename(rettv->vval.v_string); // simplify in place
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002866 rettv->v_type = VAR_STRING;
2867}
2868#endif // FEAT_EVAL