blob: e8b3fa656dc11a365171ca8659e52d2518459cb5 [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;
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002097 sctx_T save_sctx = current_sctx;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002098
2099 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002100 current_sctx = curbuf->b_p_script_ctx[BV_INEX];
2101
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002102 res = eval_to_string_safe(curbuf->b_p_inex,
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002103 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL), TRUE);
2104
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002105 set_vim_var_string(VV_FNAME, NULL, 0);
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002106 current_sctx = save_sctx;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002107 return res;
2108}
2109# endif
2110
2111/*
2112 * Return the name of the file ptr[len] in 'path'.
2113 * Otherwise like file_name_at_cursor().
2114 */
2115 char_u *
2116find_file_name_in_path(
2117 char_u *ptr,
2118 int len,
2119 int options,
2120 long count,
2121 char_u *rel_fname) // file we are searching relative to
2122{
2123 char_u *file_name;
2124 int c;
2125# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2126 char_u *tofree = NULL;
Bram Moolenaar615ddd52021-11-17 18:00:31 +00002127# endif
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002128
Bram Moolenaar615ddd52021-11-17 18:00:31 +00002129 if (len == 0)
2130 return NULL;
2131
2132# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002133 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
2134 {
2135 tofree = eval_includeexpr(ptr, len);
2136 if (tofree != NULL)
2137 {
2138 ptr = tofree;
2139 len = (int)STRLEN(ptr);
2140 }
2141 }
2142# endif
2143
2144 if (options & FNAME_EXP)
2145 {
2146 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
2147 TRUE, rel_fname);
2148
2149# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2150 /*
2151 * If the file could not be found in a normal way, try applying
2152 * 'includeexpr' (unless done already).
2153 */
2154 if (file_name == NULL
2155 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
2156 {
2157 tofree = eval_includeexpr(ptr, len);
2158 if (tofree != NULL)
2159 {
2160 ptr = tofree;
2161 len = (int)STRLEN(ptr);
2162 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
2163 TRUE, rel_fname);
2164 }
2165 }
2166# endif
2167 if (file_name == NULL && (options & FNAME_MESS))
2168 {
2169 c = ptr[len];
2170 ptr[len] = NUL;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002171 semsg(_(e_cant_find_file_str_in_path_2), ptr);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002172 ptr[len] = c;
2173 }
2174
2175 // Repeat finding the file "count" times. This matters when it
2176 // appears several times in the path.
2177 while (file_name != NULL && --count > 0)
2178 {
2179 vim_free(file_name);
2180 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
2181 }
2182 }
2183 else
2184 file_name = vim_strnsave(ptr, len);
2185
2186# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2187 vim_free(tofree);
2188# endif
2189
2190 return file_name;
2191}
2192
2193/*
2194 * Return the end of the directory name, on the first path
2195 * separator:
2196 * "/path/file", "/path/dir/", "/path//dir", "/file"
2197 * ^ ^ ^ ^
2198 */
2199 static char_u *
2200gettail_dir(char_u *fname)
2201{
2202 char_u *dir_end = fname;
2203 char_u *next_dir_end = fname;
2204 int look_for_sep = TRUE;
2205 char_u *p;
2206
2207 for (p = fname; *p != NUL; )
2208 {
2209 if (vim_ispathsep(*p))
2210 {
2211 if (look_for_sep)
2212 {
2213 next_dir_end = p;
2214 look_for_sep = FALSE;
2215 }
2216 }
2217 else
2218 {
2219 if (!look_for_sep)
2220 dir_end = next_dir_end;
2221 look_for_sep = TRUE;
2222 }
2223 MB_PTR_ADV(p);
2224 }
2225 return dir_end;
2226}
2227
2228/*
2229 * return TRUE if 'c' is a path list separator.
2230 */
2231 int
2232vim_ispathlistsep(int c)
2233{
2234# ifdef UNIX
2235 return (c == ':');
2236# else
2237 return (c == ';'); // might not be right for every system...
2238# endif
2239}
2240
2241/*
2242 * Moves "*psep" back to the previous path separator in "path".
2243 * Returns FAIL is "*psep" ends up at the beginning of "path".
2244 */
2245 static int
2246find_previous_pathsep(char_u *path, char_u **psep)
2247{
2248 // skip the current separator
2249 if (*psep > path && vim_ispathsep(**psep))
2250 --*psep;
2251
2252 // find the previous separator
2253 while (*psep > path)
2254 {
2255 if (vim_ispathsep(**psep))
2256 return OK;
2257 MB_PTR_BACK(path, *psep);
2258 }
2259
2260 return FAIL;
2261}
2262
2263/*
2264 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
2265 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
2266 */
2267 static int
2268is_unique(char_u *maybe_unique, garray_T *gap, int i)
2269{
2270 int j;
2271 int candidate_len;
2272 int other_path_len;
2273 char_u **other_paths = (char_u **)gap->ga_data;
2274 char_u *rival;
2275
2276 for (j = 0; j < gap->ga_len; j++)
2277 {
2278 if (j == i)
2279 continue; // don't compare it with itself
2280
2281 candidate_len = (int)STRLEN(maybe_unique);
2282 other_path_len = (int)STRLEN(other_paths[j]);
2283 if (other_path_len < candidate_len)
2284 continue; // it's different when it's shorter
2285
2286 rival = other_paths[j] + other_path_len - candidate_len;
2287 if (fnamecmp(maybe_unique, rival) == 0
2288 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
2289 return FALSE; // match
2290 }
2291
2292 return TRUE; // no match found
2293}
2294
2295/*
2296 * Split the 'path' option into an array of strings in garray_T. Relative
2297 * paths are expanded to their equivalent fullpath. This includes the "."
2298 * (relative to current buffer directory) and empty path (relative to current
2299 * directory) notations.
2300 *
2301 * TODO: handle upward search (;) and path limiter (**N) notations by
2302 * expanding each into their equivalent path(s).
2303 */
2304 static void
2305expand_path_option(char_u *curdir, garray_T *gap)
2306{
2307 char_u *path_option = *curbuf->b_p_path == NUL
2308 ? p_path : curbuf->b_p_path;
2309 char_u *buf;
2310 char_u *p;
2311 int len;
2312
Bram Moolenaar51e14382019-05-25 20:21:28 +02002313 if ((buf = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002314 return;
2315
2316 while (*path_option != NUL)
2317 {
2318 copy_option_part(&path_option, buf, MAXPATHL, " ,");
2319
2320 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
2321 {
2322 // Relative to current buffer:
2323 // "/path/file" + "." -> "/path/"
2324 // "/path/file" + "./subdir" -> "/path/subdir"
2325 if (curbuf->b_ffname == NULL)
2326 continue;
2327 p = gettail(curbuf->b_ffname);
2328 len = (int)(p - curbuf->b_ffname);
2329 if (len + (int)STRLEN(buf) >= MAXPATHL)
2330 continue;
2331 if (buf[1] == NUL)
2332 buf[len] = NUL;
2333 else
2334 STRMOVE(buf + len, buf + 2);
2335 mch_memmove(buf, curbuf->b_ffname, len);
2336 simplify_filename(buf);
2337 }
2338 else if (buf[0] == NUL)
2339 // relative to current directory
2340 STRCPY(buf, curdir);
2341 else if (path_with_url(buf))
2342 // URL can't be used here
2343 continue;
2344 else if (!mch_isFullName(buf))
2345 {
2346 // Expand relative path to their full path equivalent
2347 len = (int)STRLEN(curdir);
2348 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
2349 continue;
2350 STRMOVE(buf + len + 1, buf);
2351 STRCPY(buf, curdir);
2352 buf[len] = PATHSEP;
2353 simplify_filename(buf);
2354 }
2355
2356 if (ga_grow(gap, 1) == FAIL)
2357 break;
2358
2359# if defined(MSWIN)
2360 // Avoid the path ending in a backslash, it fails when a comma is
2361 // appended.
2362 len = (int)STRLEN(buf);
2363 if (buf[len - 1] == '\\')
2364 buf[len - 1] = '/';
2365# endif
2366
2367 p = vim_strsave(buf);
2368 if (p == NULL)
2369 break;
2370 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
2371 }
2372
2373 vim_free(buf);
2374}
2375
2376/*
2377 * Returns a pointer to the file or directory name in "fname" that matches the
2378 * longest path in "ga"p, or NULL if there is no match. For example:
2379 *
2380 * path: /foo/bar/baz
2381 * fname: /foo/bar/baz/quux.txt
2382 * returns: ^this
2383 */
2384 static char_u *
2385get_path_cutoff(char_u *fname, garray_T *gap)
2386{
2387 int i;
2388 int maxlen = 0;
2389 char_u **path_part = (char_u **)gap->ga_data;
2390 char_u *cutoff = NULL;
2391
2392 for (i = 0; i < gap->ga_len; i++)
2393 {
2394 int j = 0;
2395
2396 while ((fname[j] == path_part[i][j]
2397# if defined(MSWIN)
2398 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
2399# endif
2400 ) && fname[j] != NUL && path_part[i][j] != NUL)
2401 j++;
2402 if (j > maxlen)
2403 {
2404 maxlen = j;
2405 cutoff = &fname[j];
2406 }
2407 }
2408
2409 // skip to the file or directory name
2410 if (cutoff != NULL)
2411 while (vim_ispathsep(*cutoff))
2412 MB_PTR_ADV(cutoff);
2413
2414 return cutoff;
2415}
2416
2417/*
2418 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
2419 * that they are unique with respect to each other while conserving the part
2420 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
2421 */
2422 void
2423uniquefy_paths(garray_T *gap, char_u *pattern)
2424{
2425 int i;
2426 int len;
2427 char_u **fnames = (char_u **)gap->ga_data;
2428 int sort_again = FALSE;
2429 char_u *pat;
2430 char_u *file_pattern;
2431 char_u *curdir;
2432 regmatch_T regmatch;
2433 garray_T path_ga;
2434 char_u **in_curdir = NULL;
2435 char_u *short_name;
2436
2437 remove_duplicates(gap);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002438 ga_init2(&path_ga, sizeof(char_u *), 1);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002439
2440 /*
2441 * We need to prepend a '*' at the beginning of file_pattern so that the
2442 * regex matches anywhere in the path. FIXME: is this valid for all
2443 * possible patterns?
2444 */
2445 len = (int)STRLEN(pattern);
2446 file_pattern = alloc(len + 2);
2447 if (file_pattern == NULL)
2448 return;
2449 file_pattern[0] = '*';
2450 file_pattern[1] = NUL;
2451 STRCAT(file_pattern, pattern);
2452 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
2453 vim_free(file_pattern);
2454 if (pat == NULL)
2455 return;
2456
2457 regmatch.rm_ic = TRUE; // always ignore case
2458 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2459 vim_free(pat);
2460 if (regmatch.regprog == NULL)
2461 return;
2462
Bram Moolenaar51e14382019-05-25 20:21:28 +02002463 if ((curdir = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002464 goto theend;
2465 mch_dirname(curdir, MAXPATHL);
2466 expand_path_option(curdir, &path_ga);
2467
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002468 in_curdir = ALLOC_CLEAR_MULT(char_u *, gap->ga_len);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002469 if (in_curdir == NULL)
2470 goto theend;
2471
2472 for (i = 0; i < gap->ga_len && !got_int; i++)
2473 {
2474 char_u *path = fnames[i];
2475 int is_in_curdir;
2476 char_u *dir_end = gettail_dir(path);
2477 char_u *pathsep_p;
2478 char_u *path_cutoff;
2479
2480 len = (int)STRLEN(path);
2481 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
2482 && curdir[dir_end - path] == NUL;
2483 if (is_in_curdir)
2484 in_curdir[i] = vim_strsave(path);
2485
2486 // Shorten the filename while maintaining its uniqueness
2487 path_cutoff = get_path_cutoff(path, &path_ga);
2488
2489 // Don't assume all files can be reached without path when search
2490 // pattern starts with star star slash, so only remove path_cutoff
2491 // when possible.
2492 if (pattern[0] == '*' && pattern[1] == '*'
2493 && vim_ispathsep_nocolon(pattern[2])
2494 && path_cutoff != NULL
2495 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
2496 && is_unique(path_cutoff, gap, i))
2497 {
2498 sort_again = TRUE;
2499 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
2500 }
2501 else
2502 {
2503 // Here all files can be reached without path, so get shortest
2504 // unique path. We start at the end of the path.
2505 pathsep_p = path + len - 1;
2506
2507 while (find_previous_pathsep(path, &pathsep_p))
2508 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
2509 && is_unique(pathsep_p + 1, gap, i)
2510 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
2511 {
2512 sort_again = TRUE;
2513 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
2514 break;
2515 }
2516 }
2517
2518 if (mch_isFullName(path))
2519 {
2520 /*
2521 * Last resort: shorten relative to curdir if possible.
2522 * 'possible' means:
2523 * 1. It is under the current directory.
2524 * 2. The result is actually shorter than the original.
2525 *
2526 * Before curdir After
2527 * /foo/bar/file.txt /foo/bar ./file.txt
2528 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
2529 * /file.txt / /file.txt
2530 * c:\file.txt c:\ .\file.txt
2531 */
2532 short_name = shorten_fname(path, curdir);
2533 if (short_name != NULL && short_name > path + 1
2534# if defined(MSWIN)
2535 // On windows,
2536 // shorten_fname("c:\a\a.txt", "c:\a\b")
2537 // returns "\a\a.txt", which is not really the short
2538 // name, hence:
2539 && !vim_ispathsep(*short_name)
2540# endif
2541 )
2542 {
2543 STRCPY(path, ".");
2544 add_pathsep(path);
2545 STRMOVE(path + STRLEN(path), short_name);
2546 }
2547 }
2548 ui_breakcheck();
2549 }
2550
2551 // Shorten filenames in /in/current/directory/{filename}
2552 for (i = 0; i < gap->ga_len && !got_int; i++)
2553 {
2554 char_u *rel_path;
2555 char_u *path = in_curdir[i];
2556
2557 if (path == NULL)
2558 continue;
2559
2560 // If the {filename} is not unique, change it to ./{filename}.
2561 // Else reduce it to {filename}
2562 short_name = shorten_fname(path, curdir);
2563 if (short_name == NULL)
2564 short_name = path;
2565 if (is_unique(short_name, gap, i))
2566 {
2567 STRCPY(fnames[i], short_name);
2568 continue;
2569 }
2570
Bram Moolenaar51e14382019-05-25 20:21:28 +02002571 rel_path = alloc(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002572 if (rel_path == NULL)
2573 goto theend;
2574 STRCPY(rel_path, ".");
2575 add_pathsep(rel_path);
2576 STRCAT(rel_path, short_name);
2577
2578 vim_free(fnames[i]);
2579 fnames[i] = rel_path;
2580 sort_again = TRUE;
2581 ui_breakcheck();
2582 }
2583
2584theend:
2585 vim_free(curdir);
2586 if (in_curdir != NULL)
2587 {
2588 for (i = 0; i < gap->ga_len; i++)
2589 vim_free(in_curdir[i]);
2590 vim_free(in_curdir);
2591 }
2592 ga_clear_strings(&path_ga);
2593 vim_regfree(regmatch.regprog);
2594
2595 if (sort_again)
2596 remove_duplicates(gap);
2597}
2598
2599/*
2600 * Calls globpath() with 'path' values for the given pattern and stores the
2601 * result in "gap".
2602 * Returns the total number of matches.
2603 */
2604 int
2605expand_in_path(
2606 garray_T *gap,
2607 char_u *pattern,
2608 int flags) // EW_* flags
2609{
2610 char_u *curdir;
2611 garray_T path_ga;
2612 char_u *paths = NULL;
2613 int glob_flags = 0;
2614
Bram Moolenaar964b3742019-05-24 18:54:09 +02002615 if ((curdir = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002616 return 0;
2617 mch_dirname(curdir, MAXPATHL);
2618
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002619 ga_init2(&path_ga, sizeof(char_u *), 1);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002620 expand_path_option(curdir, &path_ga);
2621 vim_free(curdir);
2622 if (path_ga.ga_len == 0)
2623 return 0;
2624
2625 paths = ga_concat_strings(&path_ga, ",");
2626 ga_clear_strings(&path_ga);
2627 if (paths == NULL)
2628 return 0;
2629
2630 if (flags & EW_ICASE)
2631 glob_flags |= WILD_ICASE;
2632 if (flags & EW_ADDSLASH)
2633 glob_flags |= WILD_ADD_SLASH;
2634 globpath(paths, pattern, gap, glob_flags);
2635 vim_free(paths);
2636
2637 return gap->ga_len;
2638}
2639
2640#endif // FEAT_SEARCHPATH
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002641
2642/*
2643 * Converts a file name into a canonical form. It simplifies a file name into
2644 * its simplest form by stripping out unneeded components, if any. The
2645 * resulting file name is simplified in place and will either be the same
2646 * length as that supplied, or shorter.
2647 */
2648 void
2649simplify_filename(char_u *filename)
2650{
2651#ifndef AMIGA // Amiga doesn't have "..", it uses "/"
2652 int components = 0;
2653 char_u *p, *tail, *start;
2654 int stripping_disabled = FALSE;
2655 int relative = TRUE;
2656
2657 p = filename;
2658# ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmanan6df0f272021-12-16 13:06:10 +00002659 if (p[0] != NUL && p[1] == ':') // skip "x:"
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002660 p += 2;
2661# endif
2662
2663 if (vim_ispathsep(*p))
2664 {
2665 relative = FALSE;
2666 do
2667 ++p;
2668 while (vim_ispathsep(*p));
2669 }
2670 start = p; // remember start after "c:/" or "/" or "///"
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02002671#ifdef UNIX
2672 // Posix says that "//path" is unchanged but "///path" is "/path".
2673 if (start > filename + 2)
2674 {
2675 STRMOVE(filename + 1, p);
2676 start = p = filename + 1;
2677 }
2678#endif
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002679
2680 do
2681 {
2682 // At this point "p" is pointing to the char following a single "/"
2683 // or "p" is at the "start" of the (absolute or relative) path name.
2684# ifdef VMS
2685 // VMS allows device:[path] - don't strip the [ in directory
2686 if ((*p == '[' || *p == '<') && p > filename && p[-1] == ':')
2687 {
2688 // :[ or :< composition: vms directory component
2689 ++components;
2690 p = getnextcomp(p + 1);
2691 }
2692 // allow remote calls as host"user passwd"::device:[path]
2693 else if (p[0] == ':' && p[1] == ':' && p > filename && p[-1] == '"' )
2694 {
2695 // ":: composition: vms host/passwd component
2696 ++components;
2697 p = getnextcomp(p + 2);
2698 }
2699 else
2700# endif
2701 if (vim_ispathsep(*p))
2702 STRMOVE(p, p + 1); // remove duplicate "/"
2703 else if (p[0] == '.' && (vim_ispathsep(p[1]) || p[1] == NUL))
2704 {
2705 if (p == start && relative)
2706 p += 1 + (p[1] != NUL); // keep single "." or leading "./"
2707 else
2708 {
2709 // Strip "./" or ".///". If we are at the end of the file name
2710 // and there is no trailing path separator, either strip "/." if
2711 // we are after "start", or strip "." if we are at the beginning
2712 // of an absolute path name .
2713 tail = p + 1;
2714 if (p[1] != NUL)
2715 while (vim_ispathsep(*tail))
2716 MB_PTR_ADV(tail);
2717 else if (p > start)
2718 --p; // strip preceding path separator
2719 STRMOVE(p, tail);
2720 }
2721 }
2722 else if (p[0] == '.' && p[1] == '.' &&
2723 (vim_ispathsep(p[2]) || p[2] == NUL))
2724 {
2725 // Skip to after ".." or "../" or "..///".
2726 tail = p + 2;
2727 while (vim_ispathsep(*tail))
2728 MB_PTR_ADV(tail);
2729
2730 if (components > 0) // strip one preceding component
2731 {
2732 int do_strip = FALSE;
2733 char_u saved_char;
2734 stat_T st;
2735
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002736 // Don't strip for an erroneous file name.
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002737 if (!stripping_disabled)
2738 {
2739 // If the preceding component does not exist in the file
2740 // system, we strip it. On Unix, we don't accept a symbolic
2741 // link that refers to a non-existent file.
2742 saved_char = p[-1];
2743 p[-1] = NUL;
2744# ifdef UNIX
2745 if (mch_lstat((char *)filename, &st) < 0)
2746# else
2747 if (mch_stat((char *)filename, &st) < 0)
2748# endif
2749 do_strip = TRUE;
2750 p[-1] = saved_char;
2751
2752 --p;
2753 // Skip back to after previous '/'.
2754 while (p > start && !after_pathsep(start, p))
2755 MB_PTR_BACK(start, p);
2756
2757 if (!do_strip)
2758 {
2759 // If the component exists in the file system, check
2760 // that stripping it won't change the meaning of the
2761 // file name. First get information about the
2762 // unstripped file name. This may fail if the component
2763 // to strip is not a searchable directory (but a regular
2764 // file, for instance), since the trailing "/.." cannot
2765 // be applied then. We don't strip it then since we
2766 // don't want to replace an erroneous file name by
2767 // a valid one, and we disable stripping of later
2768 // components.
2769 saved_char = *tail;
2770 *tail = NUL;
2771 if (mch_stat((char *)filename, &st) >= 0)
2772 do_strip = TRUE;
2773 else
2774 stripping_disabled = TRUE;
2775 *tail = saved_char;
2776# ifdef UNIX
2777 if (do_strip)
2778 {
2779 stat_T new_st;
2780
2781 // On Unix, the check for the unstripped file name
2782 // above works also for a symbolic link pointing to
2783 // a searchable directory. But then the parent of
2784 // the directory pointed to by the link must be the
2785 // same as the stripped file name. (The latter
2786 // exists in the file system since it is the
2787 // component's parent directory.)
2788 if (p == start && relative)
2789 (void)mch_stat(".", &new_st);
2790 else
2791 {
2792 saved_char = *p;
2793 *p = NUL;
2794 (void)mch_stat((char *)filename, &new_st);
2795 *p = saved_char;
2796 }
2797
2798 if (new_st.st_ino != st.st_ino ||
2799 new_st.st_dev != st.st_dev)
2800 {
2801 do_strip = FALSE;
2802 // We don't disable stripping of later
2803 // components since the unstripped path name is
2804 // still valid.
2805 }
2806 }
2807# endif
2808 }
2809 }
2810
2811 if (!do_strip)
2812 {
2813 // Skip the ".." or "../" and reset the counter for the
2814 // components that might be stripped later on.
2815 p = tail;
2816 components = 0;
2817 }
2818 else
2819 {
2820 // Strip previous component. If the result would get empty
2821 // and there is no trailing path separator, leave a single
2822 // "." instead. If we are at the end of the file name and
2823 // there is no trailing path separator and a preceding
2824 // component is left after stripping, strip its trailing
2825 // path separator as well.
2826 if (p == start && relative && tail[-1] == '.')
2827 {
2828 *p++ = '.';
2829 *p = NUL;
2830 }
2831 else
2832 {
2833 if (p > start && tail[-1] == '.')
2834 --p;
2835 STRMOVE(p, tail); // strip previous component
2836 }
2837
2838 --components;
2839 }
2840 }
2841 else if (p == start && !relative) // leading "/.." or "/../"
2842 STRMOVE(p, tail); // strip ".." or "../"
2843 else
2844 {
2845 if (p == start + 2 && p[-2] == '.') // leading "./../"
2846 {
2847 STRMOVE(p - 2, p); // strip leading "./"
2848 tail -= 2;
2849 }
2850 p = tail; // skip to char after ".." or "../"
2851 }
2852 }
2853 else
2854 {
2855 ++components; // simple path component
2856 p = getnextcomp(p);
2857 }
2858 } while (*p != NUL);
2859#endif // !AMIGA
2860}
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002861
2862#if defined(FEAT_EVAL) || defined(PROTO)
2863/*
2864 * "simplify()" function
2865 */
2866 void
2867f_simplify(typval_T *argvars, typval_T *rettv)
2868{
2869 char_u *p;
2870
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002871 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2872 return;
2873
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02002874 p = tv_get_string_strict(&argvars[0]);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002875 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002876 simplify_filename(rettv->vval.v_string); // simplify in place
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002877 rettv->v_type = VAR_STRING;
2878}
2879#endif // FEAT_EVAL