blob: fd2d69f29840455188f537d6a3980b438f6e2d6c [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 * ==========
Christian Brabandtee17b6f2023-09-09 11:23:50 +020051 * Also we use an allocated search context here, these functions are NOT
52 * thread-safe!
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010053 *
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
John Marriottd6e3c902025-02-18 20:45:48 +010069 string_T ffs_fix_path;
70 string_T ffs_wc_path;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010071
72 // files/dirs found in the above directory, matched by the first wildcard
73 // of wc_part
74 char_u **ffs_filearray;
75 int ffs_filearray_size;
John Drouhard95fca122022-08-01 11:38:17 +010076 int ffs_filearray_cur; // needed for partly handled dirs
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010077
78 // to store status of partly handled directories
79 // 0: we work on this directory for the first time
80 // 1: this directory was partly searched in an earlier step
81 int ffs_stage;
82
83 // How deep are we in the directory tree?
84 // Counts backward from value of level parameter to vim_findfile_init
85 int ffs_level;
86
87 // Did we already expand '**' to an empty string?
88 int ffs_star_star_empty;
89} ff_stack_T;
90
91/*
92 * type for already visited directories or files.
93 */
94typedef struct ff_visited
95{
96 struct ff_visited *ffv_next;
97
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010098 // Visited directories are different if the wildcard string are
99 // different. So we have to save it.
100 char_u *ffv_wc_path;
Bram Moolenaar2bd9dbc2022-08-25 18:12:06 +0100101
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100102 // for unix use inode etc for comparison (needed because of links), else
103 // use filename.
104#ifdef UNIX
105 int ffv_dev_valid; // ffv_dev and ffv_ino were set
106 dev_t ffv_dev; // device number
107 ino_t ffv_ino; // inode number
108#endif
109 // The memory for this struct is allocated according to the length of
110 // ffv_fname.
111 char_u ffv_fname[1]; // actually longer
112} ff_visited_T;
113
114/*
115 * We might have to manage several visited lists during a search.
116 * This is especially needed for the tags option. If tags is set to:
117 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
118 * So we have to do 3 searches:
119 * 1) search from the current files directory downward for the file "tags"
120 * 2) search from the current files directory downward for the file "TAGS"
121 * 3) search from Vims current directory downwards for the file "tags"
122 * As you can see, the first and the third search are for the same file, so for
123 * the third search we can use the visited list of the first search. For the
124 * second search we must start from a empty visited list.
125 * The struct ff_visited_list_hdr is used to manage a linked list of already
126 * visited lists.
127 */
128typedef struct ff_visited_list_hdr
129{
130 struct ff_visited_list_hdr *ffvl_next;
131
132 // the filename the attached visited list is for
133 char_u *ffvl_filename;
134
135 ff_visited_T *ffvl_visited_list;
136
137} ff_visited_list_hdr_T;
138
139
140/*
141 * '**' can be expanded to several directory levels.
142 * Set the default maximum depth.
143 */
144#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
145
146/*
147 * The search context:
148 * ffsc_stack_ptr: the stack for the dirs to search
149 * ffsc_visited_list: the currently active visited list
150 * ffsc_dir_visited_list: the currently active visited list for search dirs
151 * ffsc_visited_lists_list: the list of all visited lists
152 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
153 * ffsc_file_to_search: the file to search for
154 * ffsc_start_dir: the starting directory, if search path was relative
155 * ffsc_fix_path: the fix part of the given path (without wildcards)
156 * Needed for upward search.
157 * ffsc_wc_path: the part of the given path containing wildcards
158 * ffsc_level: how many levels of dirs to search downwards
159 * ffsc_stopdirs_v: array of stop directories for upward search
160 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
161 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
162 */
163typedef struct ff_search_ctx_T
164{
165 ff_stack_T *ffsc_stack_ptr;
166 ff_visited_list_hdr_T *ffsc_visited_list;
167 ff_visited_list_hdr_T *ffsc_dir_visited_list;
168 ff_visited_list_hdr_T *ffsc_visited_lists_list;
169 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
John Marriottd6e3c902025-02-18 20:45:48 +0100170 string_T ffsc_file_to_search;
171 string_T ffsc_start_dir;
172 string_T ffsc_fix_path;
173 string_T ffsc_wc_path;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100174 int ffsc_level;
John Marriottd6e3c902025-02-18 20:45:48 +0100175 string_T *ffsc_stopdirs_v;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100176 int ffsc_find_what;
177 int ffsc_tagfile;
178} ff_search_ctx_T;
179
180// locally needed functions
John Marriottd6e3c902025-02-18 20:45:48 +0100181static int ff_check_visited(ff_visited_T **, char_u *, size_t, char_u *, size_t);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200182static void vim_findfile_free_visited(void *search_ctx_arg);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100183static void vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp);
184static void ff_free_visited_list(ff_visited_T *vl);
John Marriottd6e3c902025-02-18 20:45:48 +0100185static ff_visited_list_hdr_T* ff_get_visited_list(char_u *, size_t, ff_visited_list_hdr_T **list_headp);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100186
187static void ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr);
188static ff_stack_T *ff_pop(ff_search_ctx_T *search_ctx);
189static void ff_clear(ff_search_ctx_T *search_ctx);
190static void ff_free_stack_element(ff_stack_T *stack_ptr);
John Marriottd6e3c902025-02-18 20:45:48 +0100191static ff_stack_T *ff_create_stack_element(char_u *, size_t, char_u *, size_t, int, int);
192static int ff_path_in_stoplist(char_u *, int, string_T *);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100193
John Marriottd6e3c902025-02-18 20:45:48 +0100194static string_T ff_expand_buffer = {NULL, 0}; // used for expanding filenames
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100195
196#if 0
197/*
198 * if someone likes findfirst/findnext, here are the functions
199 * NOT TESTED!!
200 */
201
202static void *ff_fn_search_context = NULL;
203
204 char_u *
205vim_findfirst(char_u *path, char_u *filename, int level)
206{
207 ff_fn_search_context =
208 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
209 ff_fn_search_context, rel_fname);
210 if (NULL == ff_fn_search_context)
211 return NULL;
212 else
213 return vim_findnext()
214}
215
216 char_u *
217vim_findnext(void)
218{
219 char_u *ret = vim_findfile(ff_fn_search_context);
220
221 if (NULL == ret)
222 {
223 vim_findfile_cleanup(ff_fn_search_context);
224 ff_fn_search_context = NULL;
225 }
226 return ret;
227}
228#endif
229
230/*
231 * Initialization routine for vim_findfile().
232 *
233 * Returns the newly allocated search context or NULL if an error occurred.
234 *
235 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
236 * with the search context.
237 *
238 * Find the file 'filename' in the directory 'path'.
239 * The parameter 'path' may contain wildcards. If so only search 'level'
240 * directories deep. The parameter 'level' is the absolute maximum and is
241 * not related to restricts given to the '**' wildcard. If 'level' is 100
242 * and you use '**200' vim_findfile() will stop after 100 levels.
243 *
244 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
245 * escape special characters.
246 *
247 * If 'stopdirs' is not NULL and nothing is found downward, the search is
248 * restarted on the next higher directory level. This is repeated until the
249 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
250 * format ";*<dirname>*\(;<dirname>\)*;\=$".
251 *
252 * If the 'path' is relative, the starting dir for the search is either VIM's
253 * current dir or if the path starts with "./" the current files dir.
254 * If the 'path' is absolute, the starting dir is that part of the path before
255 * the first wildcard.
256 *
257 * Upward search is only done on the starting dir.
258 *
259 * If 'free_visited' is TRUE the list of already visited files/directories is
260 * cleared. Set this to FALSE if you just want to search from another
261 * directory, but want to be sure that no directory from a previous search is
262 * searched again. This is useful if you search for a file at different places.
263 * The list of visited files/dirs can also be cleared with the function
264 * vim_findfile_free_visited().
265 *
266 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
267 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
268 *
269 * A search context returned by a previous call to vim_findfile_init() can be
270 * passed in the parameter "search_ctx_arg". This context is reused and
271 * reinitialized with the new parameters. The list of already visited
272 * directories from this context is only deleted if the parameter
273 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
274 * if the reinitialization fails.
275 *
276 * If you don't have a search context from a previous call "search_ctx_arg"
277 * must be NULL.
278 *
279 * This function silently ignores a few errors, vim_findfile() will have
280 * limited functionality then.
281 */
282 void *
283vim_findfile_init(
284 char_u *path,
285 char_u *filename,
John Marriottd6e3c902025-02-18 20:45:48 +0100286 size_t filenamelen,
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100287 char_u *stopdirs UNUSED,
288 int level,
289 int free_visited,
290 int find_what,
291 void *search_ctx_arg,
292 int tagfile, // expanding names of tags files
293 char_u *rel_fname) // file name to use for "."
294{
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100295 char_u *wc_part;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100296 ff_stack_T *sptr;
297 ff_search_ctx_T *search_ctx;
John Marriottd6e3c902025-02-18 20:45:48 +0100298 int add_sep;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100299
300 // If a search context is given by the caller, reuse it, else allocate a
301 // new one.
302 if (search_ctx_arg != NULL)
303 search_ctx = search_ctx_arg;
304 else
305 {
Bram Moolenaara80faa82020-04-12 19:37:17 +0200306 search_ctx = ALLOC_CLEAR_ONE(ff_search_ctx_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100307 if (search_ctx == NULL)
308 goto error_return;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100309 }
310 search_ctx->ffsc_find_what = find_what;
311 search_ctx->ffsc_tagfile = tagfile;
312
313 // clear the search context, but NOT the visited lists
314 ff_clear(search_ctx);
315
316 // clear visited list if wanted
317 if (free_visited == TRUE)
318 vim_findfile_free_visited(search_ctx);
319 else
320 {
321 // Reuse old visited lists. Get the visited list for the given
322 // filename. If no list for the current filename exists, creates a new
323 // one.
324 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
John Marriottd6e3c902025-02-18 20:45:48 +0100325 filenamelen, &search_ctx->ffsc_visited_lists_list);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100326 if (search_ctx->ffsc_visited_list == NULL)
327 goto error_return;
328 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
John Marriottd6e3c902025-02-18 20:45:48 +0100329 filenamelen, &search_ctx->ffsc_dir_visited_lists_list);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100330 if (search_ctx->ffsc_dir_visited_list == NULL)
331 goto error_return;
332 }
333
John Marriottd6e3c902025-02-18 20:45:48 +0100334 if (ff_expand_buffer.string == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100335 {
John Marriottd6e3c902025-02-18 20:45:48 +0100336 ff_expand_buffer.length = 0;
337 ff_expand_buffer.string = alloc(MAXPATHL);
338 if (ff_expand_buffer.string == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100339 goto error_return;
340 }
341
342 // Store information on starting dir now if path is relative.
343 // If path is absolute, we do that later.
344 if (path[0] == '.'
345 && (vim_ispathsep(path[1]) || path[1] == NUL)
346 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
347 && rel_fname != NULL)
348 {
349 int len = (int)(gettail(rel_fname) - rel_fname);
350
351 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
352 {
353 // Make the start dir an absolute path name.
John Marriottd6e3c902025-02-18 20:45:48 +0100354 vim_strncpy(ff_expand_buffer.string, rel_fname, len);
355 ff_expand_buffer.length = len;
356
357 search_ctx->ffsc_start_dir.string = FullName_save(ff_expand_buffer.string, FALSE);
358 if (search_ctx->ffsc_start_dir.string == NULL)
359 goto error_return;
360 search_ctx->ffsc_start_dir.length = STRLEN(search_ctx->ffsc_start_dir.string);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100361 }
362 else
John Marriottd6e3c902025-02-18 20:45:48 +0100363 {
364 search_ctx->ffsc_start_dir.length = len;
365 search_ctx->ffsc_start_dir.string = vim_strnsave(rel_fname,
366 search_ctx->ffsc_start_dir.length);
367 if (search_ctx->ffsc_start_dir.string == NULL)
368 goto error_return;
369 }
370
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100371 if (*++path != NUL)
372 ++path;
373 }
374 else if (*path == NUL || !vim_isAbsName(path))
375 {
376#ifdef BACKSLASH_IN_FILENAME
377 // "c:dir" needs "c:" to be expanded, otherwise use current dir
378 if (*path != NUL && path[1] == ':')
379 {
380 char_u drive[3];
381
382 drive[0] = path[0];
383 drive[1] = ':';
384 drive[2] = NUL;
John Marriottd6e3c902025-02-18 20:45:48 +0100385 if (vim_FullName(drive, ff_expand_buffer.string, MAXPATHL, TRUE) == FAIL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100386 goto error_return;
387 path += 2;
388 }
389 else
390#endif
John Marriottd6e3c902025-02-18 20:45:48 +0100391 if (mch_dirname(ff_expand_buffer.string, MAXPATHL) == FAIL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100392 goto error_return;
393
John Marriottd6e3c902025-02-18 20:45:48 +0100394 ff_expand_buffer.length = STRLEN(ff_expand_buffer.string);
395
396 search_ctx->ffsc_start_dir.length = ff_expand_buffer.length;
397 search_ctx->ffsc_start_dir.string = vim_strnsave(ff_expand_buffer.string,
398 search_ctx->ffsc_start_dir.length);
399 if (search_ctx->ffsc_start_dir.string == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100400 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]
John Marriottd6e3c902025-02-18 20:45:48 +0100407 && search_ctx->ffsc_start_dir.string[1] == ':')
408 {
409 search_ctx->ffsc_start_dir.string[2] = NUL;
410 search_ctx->ffsc_start_dir.length = 2;
411 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100412#endif
413 }
414
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100415 /*
416 * If stopdirs are given, split them into an array of pointers.
417 * If this fails (mem allocation), there is no upward search at all or a
418 * stop directory is not recognized -> continue silently.
419 * If stopdirs just contains a ";" or is empty,
420 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
421 * is handled as unlimited upward search. See function
422 * ff_path_in_stoplist() for details.
423 */
424 if (stopdirs != NULL)
425 {
426 char_u *walker = stopdirs;
427 int dircount;
428
429 while (*walker == ';')
430 walker++;
431
432 dircount = 1;
John Marriottd6e3c902025-02-18 20:45:48 +0100433 search_ctx->ffsc_stopdirs_v = ALLOC_ONE(string_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100434
435 if (search_ctx->ffsc_stopdirs_v != NULL)
436 {
John Marriottd6e3c902025-02-18 20:45:48 +0100437 string_T *tmp; // for convenience
438
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100439 do
440 {
441 char_u *helper;
442 void *ptr;
zeertzjq764526e2024-07-11 22:24:15 +0200443 size_t len;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100444
445 helper = walker;
446 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
John Marriottd6e3c902025-02-18 20:45:48 +0100447 (dircount + 1) * sizeof(string_T));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100448 if (ptr)
449 search_ctx->ffsc_stopdirs_v = ptr;
450 else
451 // ignore, keep what we have and continue
452 break;
453 walker = vim_strchr(walker, ';');
zeertzjq764526e2024-07-11 22:24:15 +0200454 len = walker ? (size_t)(walker - helper) : STRLEN(helper);
455 // "" means ascent till top of directory tree.
John Marriottd6e3c902025-02-18 20:45:48 +0100456
zeertzjq764526e2024-07-11 22:24:15 +0200457 if (*helper != NUL && !vim_isAbsName(helper)
458 && len + 1 < MAXPATHL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100459 {
zeertzjq764526e2024-07-11 22:24:15 +0200460 // Make the stop dir an absolute path name.
John Marriottd6e3c902025-02-18 20:45:48 +0100461 vim_strncpy(ff_expand_buffer.string, helper, len);
462 ff_expand_buffer.length = len;
463
464 tmp = &search_ctx->ffsc_stopdirs_v[dircount - 1];
465 tmp->string = FullName_save(ff_expand_buffer.string, FALSE);
466 if (tmp->string != NULL)
467 tmp->length = STRLEN(tmp->string);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100468 }
469 else
John Marriottd6e3c902025-02-18 20:45:48 +0100470 {
471 tmp = &search_ctx->ffsc_stopdirs_v[dircount - 1];
472 tmp->length = len;
473 tmp->string = vim_strnsave(helper, tmp->length);
474 if (tmp->string == NULL)
475 tmp->length = 0;
476 }
zeertzjq764526e2024-07-11 22:24:15 +0200477 if (walker)
478 walker++;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100479 dircount++;
480
481 } while (walker != NULL);
John Marriottd6e3c902025-02-18 20:45:48 +0100482
483 tmp = &search_ctx->ffsc_stopdirs_v[dircount - 1];
484 tmp->string = NULL;
485 tmp->length = 0;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100486 }
487 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100488
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100489 search_ctx->ffsc_level = level;
490
491 /*
492 * split into:
493 * -fix path
494 * -wildcard_stuff (might be NULL)
495 */
496 wc_part = vim_strchr(path, '*');
497 if (wc_part != NULL)
498 {
499 int llevel;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100500 char *errpt;
501
502 // save the fix part of the path
John Marriottd6e3c902025-02-18 20:45:48 +0100503 search_ctx->ffsc_fix_path.length = (size_t)(wc_part - path);
504 search_ctx->ffsc_fix_path.string = vim_strnsave(path,
505 search_ctx->ffsc_fix_path.length);
506 if (search_ctx->ffsc_fix_path.string == NULL)
507 goto error_return;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100508
509 /*
510 * copy wc_path and add restricts to the '**' wildcard.
511 * The octet after a '**' is used as a (binary) counter.
512 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
513 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100514 * If no restrict is given after '**' the default is used.
515 * Due to this technique the path looks awful if you print it as a
516 * string.
517 */
John Marriottd6e3c902025-02-18 20:45:48 +0100518 ff_expand_buffer.length = 0;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100519 while (*wc_part != NUL)
520 {
John Marriottd6e3c902025-02-18 20:45:48 +0100521 if (ff_expand_buffer.length + 5 >= MAXPATHL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100522 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000523 emsg(_(e_path_too_long_for_completion));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100524 break;
525 }
526 if (STRNCMP(wc_part, "**", 2) == 0)
527 {
John Marriottd6e3c902025-02-18 20:45:48 +0100528 ff_expand_buffer.string[ff_expand_buffer.length++] = *wc_part++;
529 ff_expand_buffer.string[ff_expand_buffer.length++] = *wc_part++;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100530
531 llevel = strtol((char *)wc_part, &errpt, 10);
532 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
John Marriottd6e3c902025-02-18 20:45:48 +0100533 ff_expand_buffer.string[ff_expand_buffer.length++] = llevel;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100534 else if ((char_u *)errpt != wc_part && llevel == 0)
535 // restrict is 0 -> remove already added '**'
John Marriottd6e3c902025-02-18 20:45:48 +0100536 ff_expand_buffer.length -= 2;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100537 else
John Marriottd6e3c902025-02-18 20:45:48 +0100538 ff_expand_buffer.string[ff_expand_buffer.length++] = FF_MAX_STAR_STAR_EXPAND;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100539 wc_part = (char_u *)errpt;
540 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
541 {
Bram Moolenaareaaac012022-01-02 17:00:40 +0000542 semsg(_(e_invalid_path_number_must_be_at_end_of_path_or_be_followed_by_str), PATHSEPSTR);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100543 goto error_return;
544 }
545 }
546 else
John Marriottd6e3c902025-02-18 20:45:48 +0100547 ff_expand_buffer.string[ff_expand_buffer.length++] = *wc_part++;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100548 }
John Marriottd6e3c902025-02-18 20:45:48 +0100549 ff_expand_buffer.string[ff_expand_buffer.length] = NUL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100550
John Marriottd6e3c902025-02-18 20:45:48 +0100551 search_ctx->ffsc_wc_path.length = ff_expand_buffer.length;
552 search_ctx->ffsc_wc_path.string = vim_strnsave(ff_expand_buffer.string,
553 search_ctx->ffsc_wc_path.length);
554 if (search_ctx->ffsc_wc_path.string == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100555 goto error_return;
556 }
557 else
John Marriottd6e3c902025-02-18 20:45:48 +0100558 {
559 search_ctx->ffsc_fix_path.length = STRLEN(path);
560 search_ctx->ffsc_fix_path.string = vim_strnsave(path,
561 search_ctx->ffsc_fix_path.length);
562 if (search_ctx->ffsc_fix_path.string == NULL)
563 goto error_return;
564 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100565
John Marriottd6e3c902025-02-18 20:45:48 +0100566 if (search_ctx->ffsc_start_dir.string == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100567 {
568 // store the fix part as startdir.
569 // This is needed if the parameter path is fully qualified.
John Marriottd6e3c902025-02-18 20:45:48 +0100570 search_ctx->ffsc_start_dir.length = search_ctx->ffsc_fix_path.length;
571 search_ctx->ffsc_start_dir.string = vim_strnsave(search_ctx->ffsc_fix_path.string,
572 search_ctx->ffsc_start_dir.length);
573 if (search_ctx->ffsc_start_dir.string == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100574 goto error_return;
John Marriottd6e3c902025-02-18 20:45:48 +0100575 search_ctx->ffsc_fix_path.string[0] = NUL;
576 search_ctx->ffsc_fix_path.length = 0;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100577 }
578
579 // create an absolute path
John Marriottd6e3c902025-02-18 20:45:48 +0100580 if (search_ctx->ffsc_start_dir.length
581 + search_ctx->ffsc_fix_path.length + 3 >= MAXPATHL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100582 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000583 emsg(_(e_path_too_long_for_completion));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100584 goto error_return;
585 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100586
John Marriottd6e3c902025-02-18 20:45:48 +0100587 add_sep = !after_pathsep(search_ctx->ffsc_start_dir.string,
588 search_ctx->ffsc_start_dir.string + search_ctx->ffsc_start_dir.length);
589 ff_expand_buffer.length = vim_snprintf(
590 (char *)ff_expand_buffer.string,
591 MAXPATHL,
592 "%s%s",
593 search_ctx->ffsc_start_dir.string,
594 add_sep ? PATHSEPSTR : "");
595
596 {
597 size_t bufsize = ff_expand_buffer.length + search_ctx->ffsc_fix_path.length + 1;
598 char_u *buf = alloc(bufsize);
599
600 if (buf == NULL)
601 goto error_return;
602
603 vim_snprintf(
604 (char *)buf,
605 bufsize,
606 "%s%s",
607 ff_expand_buffer.string,
608 search_ctx->ffsc_fix_path.string);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100609 if (mch_isdir(buf))
610 {
John Marriottd6e3c902025-02-18 20:45:48 +0100611 if (search_ctx->ffsc_fix_path.length > 0)
612 {
613 add_sep = !after_pathsep(search_ctx->ffsc_fix_path.string,
614 search_ctx->ffsc_fix_path.string + search_ctx->ffsc_fix_path.length);
615 ff_expand_buffer.length += vim_snprintf(
616 (char *)ff_expand_buffer.string + ff_expand_buffer.length,
617 MAXPATHL - ff_expand_buffer.length,
618 "%s%s",
619 search_ctx->ffsc_fix_path.string,
620 add_sep ? PATHSEPSTR : "");
621 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100622 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100623 else
624 {
John Marriottd6e3c902025-02-18 20:45:48 +0100625 char_u *p = gettail(search_ctx->ffsc_fix_path.string);
626 int len = (int)search_ctx->ffsc_fix_path.length;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100627
John Marriottd6e3c902025-02-18 20:45:48 +0100628 if (p > search_ctx->ffsc_fix_path.string)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100629 {
Christian Brabandt7a4ca322021-07-25 15:08:05 +0200630 // do not add '..' to the path and start upwards searching
John Marriottd6e3c902025-02-18 20:45:48 +0100631 len = (int)(p - search_ctx->ffsc_fix_path.string) - 1;
Christian Brabandt7a4ca322021-07-25 15:08:05 +0200632 if ((len >= 2
John Marriottd6e3c902025-02-18 20:45:48 +0100633 && STRNCMP(search_ctx->ffsc_fix_path.string, "..", 2) == 0)
634 && (len == 2 || search_ctx->ffsc_fix_path.string[2] == PATHSEP))
Christian Brabandt7a4ca322021-07-25 15:08:05 +0200635 {
636 vim_free(buf);
637 goto error_return;
638 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100639
John Marriottd6e3c902025-02-18 20:45:48 +0100640 add_sep = !after_pathsep(search_ctx->ffsc_fix_path.string,
641 search_ctx->ffsc_fix_path.string + search_ctx->ffsc_fix_path.length);
642 ff_expand_buffer.length += vim_snprintf(
643 (char *)ff_expand_buffer.string + ff_expand_buffer.length,
644 MAXPATHL - ff_expand_buffer.length,
645 "%.*s%s",
646 len,
647 search_ctx->ffsc_fix_path.string,
648 add_sep ? PATHSEPSTR : "");
649 }
650
651 if (search_ctx->ffsc_wc_path.string != NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100652 {
John Marriottd6e3c902025-02-18 20:45:48 +0100653 size_t tempsize = (search_ctx->ffsc_fix_path.length - len)
654 + search_ctx->ffsc_wc_path.length
655 + 1;
656 char_u *temp = alloc(tempsize);
657
658 if (temp == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100659 {
660 vim_free(buf);
661 vim_free(temp);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100662 goto error_return;
663 }
664
John Marriottd6e3c902025-02-18 20:45:48 +0100665 search_ctx->ffsc_wc_path.length = vim_snprintf(
666 (char *)temp,
667 tempsize,
668 "%s%s",
669 search_ctx->ffsc_fix_path.string + len,
670 search_ctx->ffsc_wc_path.string);
671 vim_free(search_ctx->ffsc_wc_path.string);
672 search_ctx->ffsc_wc_path.string = temp;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100673 }
674 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100675 vim_free(buf);
676 }
677
John Marriottd6e3c902025-02-18 20:45:48 +0100678 sptr = ff_create_stack_element(ff_expand_buffer.string,
679 ff_expand_buffer.length,
680 search_ctx->ffsc_wc_path.string,
681 search_ctx->ffsc_wc_path.length,
682 level,
683 0);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100684
685 if (sptr == NULL)
686 goto error_return;
687
688 ff_push(search_ctx, sptr);
689
John Marriottd6e3c902025-02-18 20:45:48 +0100690 search_ctx->ffsc_file_to_search.length = filenamelen;
691 search_ctx->ffsc_file_to_search.string = vim_strnsave(filename,
692 search_ctx->ffsc_file_to_search.length);
693 if (search_ctx->ffsc_file_to_search.string == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100694 goto error_return;
695
696 return search_ctx;
697
698error_return:
699 /*
700 * We clear the search context now!
701 * Even when the caller gave us a (perhaps valid) context we free it here,
702 * as we might have already destroyed it.
703 */
704 vim_findfile_cleanup(search_ctx);
705 return NULL;
706}
707
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100708/*
709 * Get the stopdir string. Check that ';' is not escaped.
710 */
711 char_u *
712vim_findfile_stopdir(char_u *buf)
713{
714 char_u *r_ptr = buf;
John Marriottd6e3c902025-02-18 20:45:48 +0100715 char_u *r_ptr_end = NULL; // points to NUL at end of string "r_ptr"
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100716
717 while (*r_ptr != NUL && *r_ptr != ';')
718 {
719 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
720 {
721 // Overwrite the escape char,
722 // use STRLEN(r_ptr) to move the trailing '\0'.
John Marriottd6e3c902025-02-18 20:45:48 +0100723 if (r_ptr_end == NULL)
724 r_ptr_end = r_ptr + STRLEN(r_ptr);
725 mch_memmove(r_ptr, r_ptr + 1,
726 (size_t)(r_ptr_end - (r_ptr + 1)) + 1); // +1 for NUL
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100727 r_ptr++;
John Marriottd6e3c902025-02-18 20:45:48 +0100728 --r_ptr_end;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100729 }
730 r_ptr++;
731 }
732 if (*r_ptr == ';')
733 {
John Marriottd6e3c902025-02-18 20:45:48 +0100734 *r_ptr = NUL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100735 r_ptr++;
736 }
737 else if (*r_ptr == NUL)
738 r_ptr = NULL;
739 return r_ptr;
740}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100741
742/*
743 * Clean up the given search context. Can handle a NULL pointer.
744 */
745 void
746vim_findfile_cleanup(void *ctx)
747{
748 if (ctx == NULL)
749 return;
750
751 vim_findfile_free_visited(ctx);
752 ff_clear(ctx);
753 vim_free(ctx);
754}
755
756/*
757 * Find a file in a search context.
758 * The search context was created with vim_findfile_init() above.
759 * Return a pointer to an allocated file name or NULL if nothing found.
760 * To get all matching files call this function until you get NULL.
761 *
762 * If the passed search_context is NULL, NULL is returned.
763 *
764 * The search algorithm is depth first. To change this replace the
765 * stack with a list (don't forget to leave partly searched directories on the
766 * top of the list).
767 */
768 char_u *
769vim_findfile(void *search_ctx_arg)
770{
John Marriottd6e3c902025-02-18 20:45:48 +0100771 string_T file_path;
772 string_T rest_of_wildcards;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100773 char_u *path_end = NULL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100774 ff_stack_T *stackp;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100775 ff_search_ctx_T *search_ctx;
776
777 if (search_ctx_arg == NULL)
778 return NULL;
779
780 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
781
782 /*
783 * filepath is used as buffer for various actions and as the storage to
784 * return a found filename.
785 */
John Marriottd6e3c902025-02-18 20:45:48 +0100786 if ((file_path.string = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100787 return NULL;
788
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100789 // store the end of the start dir -- needed for upward search
John Marriottd6e3c902025-02-18 20:45:48 +0100790 if (search_ctx->ffsc_start_dir.string != NULL)
791 path_end = &search_ctx->ffsc_start_dir.string[
792 search_ctx->ffsc_start_dir.length];
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100793
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100794 // upward search loop
795 for (;;)
796 {
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100797 // downward search loop
798 for (;;)
799 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000800 // check if user wants to stop the search
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100801 ui_breakcheck();
802 if (got_int)
803 break;
804
805 // get directory to work on from stack
806 stackp = ff_pop(search_ctx);
807 if (stackp == NULL)
808 break;
809
810 /*
811 * TODO: decide if we leave this test in
812 *
813 * GOOD: don't search a directory(-tree) twice.
814 * BAD: - check linked list for every new directory entered.
815 * - check for double files also done below
816 *
817 * Here we check if we already searched this directory.
818 * We already searched a directory if:
819 * 1) The directory is the same.
820 * 2) We would use the same wildcard string.
821 *
822 * Good if you have links on same directory via several ways
823 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
824 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
825 *
826 * This check is only needed for directories we work on for the
827 * first time (hence stackp->ff_filearray == NULL)
828 */
829 if (stackp->ffs_filearray == NULL
830 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
831 ->ffvl_visited_list,
John Marriottd6e3c902025-02-18 20:45:48 +0100832 stackp->ffs_fix_path.string,
833 stackp->ffs_fix_path.length,
834 stackp->ffs_wc_path.string,
835 stackp->ffs_wc_path.length) == FAIL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100836 {
837#ifdef FF_VERBOSE
838 if (p_verbose >= 5)
839 {
840 verbose_enter_scroll();
841 smsg("Already Searched: %s (%s)",
John Marriottd6e3c902025-02-18 20:45:48 +0100842 stackp->ffs_fix_path.string, stackp->ffs_wc_path.string);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100843 // don't overwrite this either
844 msg_puts("\n");
845 verbose_leave_scroll();
846 }
847#endif
848 ff_free_stack_element(stackp);
849 continue;
850 }
851#ifdef FF_VERBOSE
852 else if (p_verbose >= 5)
853 {
854 verbose_enter_scroll();
855 smsg("Searching: %s (%s)",
John Marriottd6e3c902025-02-18 20:45:48 +0100856 stackp->ffs_fix_path.string, stackp->ffs_wc_path.string);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100857 // don't overwrite this either
858 msg_puts("\n");
859 verbose_leave_scroll();
860 }
861#endif
862
863 // check depth
864 if (stackp->ffs_level <= 0)
865 {
866 ff_free_stack_element(stackp);
867 continue;
868 }
869
John Marriottd6e3c902025-02-18 20:45:48 +0100870 file_path.string[0] = NUL;
871 file_path.length = 0;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100872
873 /*
874 * If no filearray till now expand wildcards
875 * The function expand_wildcards() can handle an array of paths
876 * and all possible expands are returned in one array. We use this
877 * to handle the expansion of '**' into an empty string.
878 */
879 if (stackp->ffs_filearray == NULL)
880 {
881 char_u *dirptrs[2];
882
883 // we use filepath to build the path expand_wildcards() should
884 // expand.
John Marriottd6e3c902025-02-18 20:45:48 +0100885 dirptrs[0] = file_path.string;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100886 dirptrs[1] = NULL;
887
888 // if we have a start dir copy it in
John Marriottd6e3c902025-02-18 20:45:48 +0100889 if (!vim_isAbsName(stackp->ffs_fix_path.string)
890 && search_ctx->ffsc_start_dir.string)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100891 {
John Marriottd6e3c902025-02-18 20:45:48 +0100892 if (search_ctx->ffsc_start_dir.length + 1 < MAXPATHL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100893 {
John Marriottd6e3c902025-02-18 20:45:48 +0100894 int add_sep = !after_pathsep(search_ctx->ffsc_start_dir.string,
895 search_ctx->ffsc_start_dir.string + search_ctx->ffsc_start_dir.length);
896 file_path.length = vim_snprintf(
897 (char *)file_path.string,
898 MAXPATHL,
899 "%s%s",
900 search_ctx->ffsc_start_dir.string,
901 add_sep ? PATHSEPSTR : "");
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100902 }
903 else
904 {
905 ff_free_stack_element(stackp);
906 goto fail;
907 }
908 }
909
910 // append the fix part of the search path
John Marriottd6e3c902025-02-18 20:45:48 +0100911 if (file_path.length + stackp->ffs_fix_path.length + 1 < MAXPATHL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100912 {
John Marriottd6e3c902025-02-18 20:45:48 +0100913 int add_sep = !after_pathsep(stackp->ffs_fix_path.string,
914 stackp->ffs_fix_path.string + stackp->ffs_fix_path.length);
915 file_path.length += vim_snprintf(
916 (char *)file_path.string + file_path.length,
917 MAXPATHL - file_path.length,
918 "%s%s",
919 stackp->ffs_fix_path.string,
920 add_sep ? PATHSEPSTR : "");
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100921 }
922 else
923 {
924 ff_free_stack_element(stackp);
925 goto fail;
926 }
927
John Marriottd6e3c902025-02-18 20:45:48 +0100928 rest_of_wildcards.string = stackp->ffs_wc_path.string;
929 rest_of_wildcards.length = stackp->ffs_wc_path.length;
930 if (*rest_of_wildcards.string != NUL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100931 {
John Marriottd6e3c902025-02-18 20:45:48 +0100932 if (STRNCMP(rest_of_wildcards.string, "**", 2) == 0)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100933 {
John Marriottd6e3c902025-02-18 20:45:48 +0100934 char_u *p;
935
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100936 // pointer to the restrict byte
937 // The restrict byte is not a character!
John Marriottd6e3c902025-02-18 20:45:48 +0100938 p = rest_of_wildcards.string + 2;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100939
940 if (*p > 0)
941 {
942 (*p)--;
John Marriottd6e3c902025-02-18 20:45:48 +0100943 if (file_path.length + 1 < MAXPATHL)
944 file_path.string[file_path.length++] = '*';
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100945 else
946 {
947 ff_free_stack_element(stackp);
948 goto fail;
949 }
950 }
951
952 if (*p == 0)
953 {
954 // remove '**<numb> from wildcards
John Marriottd6e3c902025-02-18 20:45:48 +0100955 mch_memmove(rest_of_wildcards.string,
956 rest_of_wildcards.string + 3,
957 (size_t)(rest_of_wildcards.length - 3) + 1); // +1 for NUL
958 rest_of_wildcards.length -= 3;
959 stackp->ffs_wc_path.length = rest_of_wildcards.length;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100960 }
961 else
John Marriottd6e3c902025-02-18 20:45:48 +0100962 {
963 rest_of_wildcards.string += 3;
964 rest_of_wildcards.length -= 3;
965 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100966
967 if (stackp->ffs_star_star_empty == 0)
968 {
969 // if not done before, expand '**' to empty
970 stackp->ffs_star_star_empty = 1;
John Marriottd6e3c902025-02-18 20:45:48 +0100971 dirptrs[1] = stackp->ffs_fix_path.string;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100972 }
973 }
974
975 /*
976 * Here we copy until the next path separator or the end of
977 * the path. If we stop at a path separator, there is
978 * still something else left. This is handled below by
979 * pushing every directory returned from expand_wildcards()
980 * on the stack again for further search.
981 */
John Marriottd6e3c902025-02-18 20:45:48 +0100982 while (*rest_of_wildcards.string
983 && !vim_ispathsep(*rest_of_wildcards.string))
984 {
985 if (file_path.length + 1 < MAXPATHL)
986 {
987 file_path.string[file_path.length++] = *rest_of_wildcards.string++;
988 --rest_of_wildcards.length;
989 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100990 else
991 {
992 ff_free_stack_element(stackp);
993 goto fail;
994 }
John Marriottd6e3c902025-02-18 20:45:48 +0100995 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100996
John Marriottd6e3c902025-02-18 20:45:48 +0100997 file_path.string[file_path.length] = NUL;
998 if (vim_ispathsep(*rest_of_wildcards.string))
999 {
1000 rest_of_wildcards.string++;
1001 rest_of_wildcards.length--;
1002 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001003 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001004
1005 /*
1006 * Expand wildcards like "*" and "$VAR".
1007 * If the path is a URL don't try this.
1008 */
1009 if (path_with_url(dirptrs[0]))
1010 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001011 stackp->ffs_filearray = ALLOC_ONE(char_u *);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001012 if (stackp->ffs_filearray != NULL
1013 && (stackp->ffs_filearray[0]
John Marriottd6e3c902025-02-18 20:45:48 +01001014 = vim_strnsave(dirptrs[0], file_path.length)) != NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001015 stackp->ffs_filearray_size = 1;
1016 else
1017 stackp->ffs_filearray_size = 0;
1018 }
1019 else
1020 // Add EW_NOTWILD because the expanded path may contain
1021 // wildcard characters that are to be taken literally.
1022 // This is a bit of a hack.
1023 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
1024 &stackp->ffs_filearray_size,
1025 &stackp->ffs_filearray,
1026 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
1027
1028 stackp->ffs_filearray_cur = 0;
1029 stackp->ffs_stage = 0;
1030 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001031 else
John Marriottd6e3c902025-02-18 20:45:48 +01001032 {
1033 rest_of_wildcards.string = &stackp->ffs_wc_path.string[
1034 stackp->ffs_wc_path.length];
1035 rest_of_wildcards.length = 0;
1036 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001037
1038 if (stackp->ffs_stage == 0)
1039 {
John Marriottd6e3c902025-02-18 20:45:48 +01001040 int i;
1041
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001042 // this is the first time we work on this directory
John Marriottd6e3c902025-02-18 20:45:48 +01001043 if (*rest_of_wildcards.string == NUL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001044 {
John Marriottd6e3c902025-02-18 20:45:48 +01001045 size_t len;
1046 char_u *suf;
1047
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001048 /*
1049 * We don't have further wildcards to expand, so we have to
1050 * check for the final file now.
1051 */
1052 for (i = stackp->ffs_filearray_cur;
1053 i < stackp->ffs_filearray_size; ++i)
1054 {
1055 if (!path_with_url(stackp->ffs_filearray[i])
1056 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 continue; // not a directory
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001058
1059 // prepare the filename to be checked for existence
1060 // below
John Marriottd6e3c902025-02-18 20:45:48 +01001061 len = STRLEN(stackp->ffs_filearray[i]);
1062 if (len + 1 + search_ctx->ffsc_file_to_search.length
1063 < MAXPATHL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001064 {
John Marriottd6e3c902025-02-18 20:45:48 +01001065 int add_sep = !after_pathsep(stackp->ffs_filearray[i],
1066 stackp->ffs_filearray[i] + len);
1067 file_path.length = vim_snprintf(
1068 (char *)file_path.string,
1069 MAXPATHL,
1070 "%s%s%s",
1071 stackp->ffs_filearray[i],
1072 add_sep ? PATHSEPSTR : "",
1073 search_ctx->ffsc_file_to_search.string);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001074 }
1075 else
1076 {
1077 ff_free_stack_element(stackp);
1078 goto fail;
1079 }
1080
1081 /*
1082 * Try without extra suffix and then with suffixes
1083 * from 'suffixesadd'.
1084 */
zeertzjqbf595ae2025-02-22 09:13:17 +01001085 len = file_path.length;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001086 if (search_ctx->ffsc_tagfile)
1087 suf = (char_u *)"";
1088 else
1089 suf = curbuf->b_p_sua;
1090 for (;;)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001091 {
1092 // if file exists and we didn't already find it
John Marriottd6e3c902025-02-18 20:45:48 +01001093 if ((path_with_url(file_path.string)
1094 || (mch_getperm(file_path.string) >= 0
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001095 && (search_ctx->ffsc_find_what
1096 == FINDFILE_BOTH
1097 || ((search_ctx->ffsc_find_what
1098 == FINDFILE_DIR)
John Marriottd6e3c902025-02-18 20:45:48 +01001099 == mch_isdir(file_path.string)))))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001100#ifndef FF_VERBOSE
1101 && (ff_check_visited(
Bram Moolenaar2bd9dbc2022-08-25 18:12:06 +01001102 &search_ctx->ffsc_visited_list
1103 ->ffvl_visited_list,
John Marriottd6e3c902025-02-18 20:45:48 +01001104 file_path.string,
1105 file_path.length,
1106 (char_u *)"", 0) == OK)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001107#endif
1108 )
1109 {
1110#ifdef FF_VERBOSE
1111 if (ff_check_visited(
Bram Moolenaar2bd9dbc2022-08-25 18:12:06 +01001112 &search_ctx->ffsc_visited_list
1113 ->ffvl_visited_list,
John Marriottd6e3c902025-02-18 20:45:48 +01001114 file_path.string,
1115 file_path.length,
1116 (char_u *)"", 0) == FAIL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001117 {
1118 if (p_verbose >= 5)
1119 {
1120 verbose_enter_scroll();
John Marriottd6e3c902025-02-18 20:45:48 +01001121 smsg("Already: %s", file_path.string);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001122 // don't overwrite this either
1123 msg_puts("\n");
1124 verbose_leave_scroll();
1125 }
1126 continue;
1127 }
1128#endif
1129
1130 // push dir to examine rest of subdirs later
1131 stackp->ffs_filearray_cur = i + 1;
1132 ff_push(search_ctx, stackp);
1133
John Marriottd6e3c902025-02-18 20:45:48 +01001134 if (!path_with_url(file_path.string))
1135 file_path.length = simplify_filename(file_path.string);
1136
1137 if (mch_dirname(ff_expand_buffer.string, MAXPATHL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001138 == OK)
1139 {
John Marriottd6e3c902025-02-18 20:45:48 +01001140 char_u *p;
1141
1142 ff_expand_buffer.length = STRLEN(ff_expand_buffer.string);
1143 p = shorten_fname(file_path.string,
1144 ff_expand_buffer.string);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001145 if (p != NULL)
John Marriottd6e3c902025-02-18 20:45:48 +01001146 {
1147 mch_memmove(file_path.string, p,
1148 (size_t)((file_path.string + file_path.length) - p) + 1); // +1 for NUL
1149 file_path.length -= (p - file_path.string);
1150 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001151 }
1152#ifdef FF_VERBOSE
1153 if (p_verbose >= 5)
1154 {
1155 verbose_enter_scroll();
John Marriottd6e3c902025-02-18 20:45:48 +01001156 smsg("HIT: %s", file_path.string);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001157 // don't overwrite this either
1158 msg_puts("\n");
1159 verbose_leave_scroll();
1160 }
1161#endif
John Marriottd6e3c902025-02-18 20:45:48 +01001162 return file_path.string;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001163 }
1164
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001165 // Not found or found already, try next suffix.
1166 if (*suf == NUL)
1167 break;
zeertzjqbf595ae2025-02-22 09:13:17 +01001168 file_path.length = len + copy_option_part(&suf,
Yegappan Lakshmanan7b6add02025-04-01 20:38:37 +02001169 file_path.string + len,
1170 (int)(MAXPATHL - len), ",");
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001171 }
1172 }
1173 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001174 else
1175 {
1176 /*
1177 * still wildcards left, push the directories for further
1178 * search
1179 */
1180 for (i = stackp->ffs_filearray_cur;
1181 i < stackp->ffs_filearray_size; ++i)
1182 {
1183 if (!mch_isdir(stackp->ffs_filearray[i]))
1184 continue; // not a directory
1185
1186 ff_push(search_ctx,
1187 ff_create_stack_element(
1188 stackp->ffs_filearray[i],
John Marriottd6e3c902025-02-18 20:45:48 +01001189 STRLEN(stackp->ffs_filearray[i]),
1190 rest_of_wildcards.string,
1191 rest_of_wildcards.length,
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001192 stackp->ffs_level - 1, 0));
1193 }
1194 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001195 stackp->ffs_filearray_cur = 0;
1196 stackp->ffs_stage = 1;
1197 }
1198
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001199 /*
1200 * if wildcards contains '**' we have to descent till we reach the
1201 * leaves of the directory tree.
1202 */
John Marriottd6e3c902025-02-18 20:45:48 +01001203 if (STRNCMP(stackp->ffs_wc_path.string, "**", 2) == 0)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001204 {
John Marriottd6e3c902025-02-18 20:45:48 +01001205 int i;
1206
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001207 for (i = stackp->ffs_filearray_cur;
1208 i < stackp->ffs_filearray_size; ++i)
1209 {
1210 if (fnamecmp(stackp->ffs_filearray[i],
John Marriottd6e3c902025-02-18 20:45:48 +01001211 stackp->ffs_fix_path.string) == 0)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001212 continue; // don't repush same directory
1213 if (!mch_isdir(stackp->ffs_filearray[i]))
1214 continue; // not a directory
1215 ff_push(search_ctx,
John Marriottd6e3c902025-02-18 20:45:48 +01001216 ff_create_stack_element(
1217 stackp->ffs_filearray[i],
1218 STRLEN(stackp->ffs_filearray[i]),
1219 stackp->ffs_wc_path.string,
1220 stackp->ffs_wc_path.length,
1221 stackp->ffs_level - 1, 1));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001222 }
1223 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001224
1225 // we are done with the current directory
1226 ff_free_stack_element(stackp);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001227 }
1228
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001229 // If we reached this, we didn't find anything downwards.
1230 // Let's check if we should do an upward search.
John Marriottd6e3c902025-02-18 20:45:48 +01001231 if (search_ctx->ffsc_start_dir.string
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001232 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
1233 {
1234 ff_stack_T *sptr;
zeertzjqe6ab23b2024-07-11 22:22:26 +02001235 // path_end may point to the NUL or the previous path separator
John Marriottd6e3c902025-02-18 20:45:48 +01001236 int plen = (path_end - search_ctx->ffsc_start_dir.string)
zeertzjqe6ab23b2024-07-11 22:22:26 +02001237 + (*path_end != NUL);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001238
1239 // is the last starting directory in the stop list?
John Marriottd6e3c902025-02-18 20:45:48 +01001240 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir.string,
zeertzjqe6ab23b2024-07-11 22:22:26 +02001241 plen, search_ctx->ffsc_stopdirs_v) == TRUE)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001242 break;
1243
1244 // cut of last dir
John Marriottd6e3c902025-02-18 20:45:48 +01001245 while (path_end > search_ctx->ffsc_start_dir.string
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001246 && vim_ispathsep(*path_end))
1247 path_end--;
John Marriottd6e3c902025-02-18 20:45:48 +01001248 while (path_end > search_ctx->ffsc_start_dir.string
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001249 && !vim_ispathsep(path_end[-1]))
1250 path_end--;
John Marriottd6e3c902025-02-18 20:45:48 +01001251 *path_end = NUL;
1252
1253 // we may have shortened search_ctx->ffsc_start_dir, so update it's length
1254 search_ctx->ffsc_start_dir.length = (size_t)(path_end - search_ctx->ffsc_start_dir.string);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001255 path_end--;
1256
John Marriottd6e3c902025-02-18 20:45:48 +01001257 if (*search_ctx->ffsc_start_dir.string == NUL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001258 break;
1259
John Marriottd6e3c902025-02-18 20:45:48 +01001260 if (search_ctx->ffsc_start_dir.length + 1
1261 + search_ctx->ffsc_fix_path.length < MAXPATHL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001262 {
John Marriottd6e3c902025-02-18 20:45:48 +01001263 int add_sep = !after_pathsep(search_ctx->ffsc_start_dir.string,
1264 search_ctx->ffsc_start_dir.string + search_ctx->ffsc_start_dir.length);
1265 file_path.length = vim_snprintf(
1266 (char *)file_path.string,
1267 MAXPATHL,
1268 "%s%s%s",
1269 search_ctx->ffsc_start_dir.string,
1270 add_sep ? PATHSEPSTR : "",
1271 search_ctx->ffsc_fix_path.string);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001272 }
1273 else
1274 goto fail;
1275
1276 // create a new stack entry
John Marriottd6e3c902025-02-18 20:45:48 +01001277 sptr = ff_create_stack_element(file_path.string, file_path.length,
1278 search_ctx->ffsc_wc_path.string, search_ctx->ffsc_wc_path.length,
1279 search_ctx->ffsc_level, 0);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001280 if (sptr == NULL)
1281 break;
1282 ff_push(search_ctx, sptr);
1283 }
1284 else
1285 break;
1286 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001287
1288fail:
John Marriottd6e3c902025-02-18 20:45:48 +01001289 vim_free(file_path.string);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001290 return NULL;
1291}
1292
1293/*
1294 * Free the list of lists of visited files and directories
1295 * Can handle it if the passed search_context is NULL;
1296 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001297 static void
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001298vim_findfile_free_visited(void *search_ctx_arg)
1299{
1300 ff_search_ctx_T *search_ctx;
1301
1302 if (search_ctx_arg == NULL)
1303 return;
1304
1305 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
1306 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
1307 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
1308}
1309
1310 static void
1311vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp)
1312{
1313 ff_visited_list_hdr_T *vp;
1314
1315 while (*list_headp != NULL)
1316 {
1317 vp = (*list_headp)->ffvl_next;
1318 ff_free_visited_list((*list_headp)->ffvl_visited_list);
1319
1320 vim_free((*list_headp)->ffvl_filename);
1321 vim_free(*list_headp);
1322 *list_headp = vp;
1323 }
1324 *list_headp = NULL;
1325}
1326
1327 static void
1328ff_free_visited_list(ff_visited_T *vl)
1329{
1330 ff_visited_T *vp;
1331
1332 while (vl != NULL)
1333 {
1334 vp = vl->ffv_next;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001335 vim_free(vl->ffv_wc_path);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001336 vim_free(vl);
1337 vl = vp;
1338 }
1339 vl = NULL;
1340}
1341
1342/*
1343 * Returns the already visited list for the given filename. If none is found it
1344 * allocates a new one.
1345 */
1346 static ff_visited_list_hdr_T*
1347ff_get_visited_list(
1348 char_u *filename,
John Marriottd6e3c902025-02-18 20:45:48 +01001349 size_t filenamelen,
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001350 ff_visited_list_hdr_T **list_headp)
1351{
1352 ff_visited_list_hdr_T *retptr = NULL;
1353
1354 // check if a visited list for the given filename exists
1355 if (*list_headp != NULL)
1356 {
1357 retptr = *list_headp;
1358 while (retptr != NULL)
1359 {
1360 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
1361 {
1362#ifdef FF_VERBOSE
1363 if (p_verbose >= 5)
1364 {
1365 verbose_enter_scroll();
1366 smsg("ff_get_visited_list: FOUND list for %s",
1367 filename);
1368 // don't overwrite this either
1369 msg_puts("\n");
1370 verbose_leave_scroll();
1371 }
1372#endif
1373 return retptr;
1374 }
1375 retptr = retptr->ffvl_next;
1376 }
1377 }
1378
1379#ifdef FF_VERBOSE
1380 if (p_verbose >= 5)
1381 {
1382 verbose_enter_scroll();
1383 smsg("ff_get_visited_list: new list for %s", filename);
1384 // don't overwrite this either
1385 msg_puts("\n");
1386 verbose_leave_scroll();
1387 }
1388#endif
1389
1390 /*
1391 * if we reach this we didn't find a list and we have to allocate new list
1392 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001393 retptr = ALLOC_ONE(ff_visited_list_hdr_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001394 if (retptr == NULL)
1395 return NULL;
1396
1397 retptr->ffvl_visited_list = NULL;
John Marriottd6e3c902025-02-18 20:45:48 +01001398 retptr->ffvl_filename = vim_strnsave(filename, filenamelen);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001399 if (retptr->ffvl_filename == NULL)
1400 {
1401 vim_free(retptr);
1402 return NULL;
1403 }
1404 retptr->ffvl_next = *list_headp;
1405 *list_headp = retptr;
1406
1407 return retptr;
1408}
1409
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001410/*
1411 * check if two wildcard paths are equal. Returns TRUE or FALSE.
1412 * They are equal if:
1413 * - both paths are NULL
1414 * - they have the same length
1415 * - char by char comparison is OK
1416 * - the only differences are in the counters behind a '**', so
1417 * '**\20' is equal to '**\24'
1418 */
1419 static int
1420ff_wc_equal(char_u *s1, char_u *s2)
1421{
1422 int i, j;
1423 int c1 = NUL;
1424 int c2 = NUL;
1425 int prev1 = NUL;
1426 int prev2 = NUL;
1427
1428 if (s1 == s2)
1429 return TRUE;
1430
1431 if (s1 == NULL || s2 == NULL)
1432 return FALSE;
1433
1434 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
1435 {
1436 c1 = PTR2CHAR(s1 + i);
1437 c2 = PTR2CHAR(s2 + j);
1438
1439 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
1440 && (prev1 != '*' || prev2 != '*'))
1441 return FALSE;
1442 prev2 = prev1;
1443 prev1 = c1;
1444
Bram Moolenaar1614a142019-10-06 22:00:13 +02001445 i += mb_ptr2len(s1 + i);
1446 j += mb_ptr2len(s2 + j);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001447 }
1448 return s1[i] == s2[j];
1449}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001450
1451/*
1452 * maintains the list of already visited files and dirs
1453 * returns FAIL if the given file/dir is already in the list
1454 * returns OK if it is newly added
1455 *
1456 * TODO: What to do on memory allocation problems?
1457 * -> return TRUE - Better the file is found several times instead of
1458 * never.
1459 */
1460 static int
1461ff_check_visited(
1462 ff_visited_T **visited_list,
Bram Moolenaar2bd9dbc2022-08-25 18:12:06 +01001463 char_u *fname,
John Marriottd6e3c902025-02-18 20:45:48 +01001464 size_t fnamelen,
1465 char_u *wc_path,
1466 size_t wc_pathlen)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001467{
1468 ff_visited_T *vp;
1469#ifdef UNIX
1470 stat_T st;
1471 int url = FALSE;
1472#endif
1473
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001474 // For a URL we only compare the name, otherwise we compare the
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001475 // device/inode (unix) or the full path name (not Unix).
1476 if (path_with_url(fname))
1477 {
John Marriottd6e3c902025-02-18 20:45:48 +01001478 vim_strncpy(ff_expand_buffer.string, fname, fnamelen);
1479 ff_expand_buffer.length = fnamelen;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001480#ifdef UNIX
1481 url = TRUE;
1482#endif
1483 }
1484 else
1485 {
John Marriottd6e3c902025-02-18 20:45:48 +01001486 ff_expand_buffer.string[0] = NUL;
1487 ff_expand_buffer.length = 0;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001488#ifdef UNIX
1489 if (mch_stat((char *)fname, &st) < 0)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001490 return FAIL;
John Marriottd6e3c902025-02-18 20:45:48 +01001491#else
1492 if (vim_FullName(fname, ff_expand_buffer.string, MAXPATHL, TRUE) == FAIL)
1493 return FAIL;
1494 ff_expand_buffer.length = STRLEN(ff_expand_buffer.string);
1495#endif
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001496 }
1497
1498 // check against list of already visited files
1499 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
1500 {
1501 if (
1502#ifdef UNIX
1503 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
1504 && vp->ffv_ino == st.st_ino)
1505 :
1506#endif
John Marriottd6e3c902025-02-18 20:45:48 +01001507 fnamecmp(vp->ffv_fname, ff_expand_buffer.string) == 0
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001508 )
1509 {
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001510 // are the wildcard parts equal
1511 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001512 // already visited
1513 return FAIL;
1514 }
1515 }
1516
1517 /*
1518 * New file/dir. Add it to the list of visited files/dirs.
1519 */
zeertzjq1b438a82023-02-01 13:11:15 +00001520 vp = alloc(
John Marriottd6e3c902025-02-18 20:45:48 +01001521 offsetof(ff_visited_T, ffv_fname) + ff_expand_buffer.length + 1);
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001522 if (vp == NULL)
1523 return OK;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001524
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001525#ifdef UNIX
1526 if (!url)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001527 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001528 vp->ffv_dev_valid = TRUE;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001529 vp->ffv_dev = st.st_dev;
John Marriottd6e3c902025-02-18 20:45:48 +01001530 vp->ffv_ino = st.st_ino;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001531 vp->ffv_fname[0] = NUL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001532 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001533 else
1534 {
1535 vp->ffv_dev_valid = FALSE;
1536#endif
John Marriottd6e3c902025-02-18 20:45:48 +01001537 STRCPY(vp->ffv_fname, ff_expand_buffer.string);
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001538#ifdef UNIX
1539 }
1540#endif
1541 if (wc_path != NULL)
John Marriottd6e3c902025-02-18 20:45:48 +01001542 vp->ffv_wc_path = vim_strnsave(wc_path, wc_pathlen);
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001543 else
1544 vp->ffv_wc_path = NULL;
1545
1546 vp->ffv_next = *visited_list;
1547 *visited_list = vp;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001548
1549 return OK;
1550}
1551
1552/*
1553 * create stack element from given path pieces
1554 */
1555 static ff_stack_T *
1556ff_create_stack_element(
1557 char_u *fix_part,
John Marriottd6e3c902025-02-18 20:45:48 +01001558 size_t fix_partlen,
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001559 char_u *wc_part,
John Marriottd6e3c902025-02-18 20:45:48 +01001560 size_t wc_partlen,
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001561 int level,
1562 int star_star_empty)
1563{
1564 ff_stack_T *new;
1565
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001566 new = ALLOC_ONE(ff_stack_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001567 if (new == NULL)
1568 return NULL;
1569
1570 new->ffs_prev = NULL;
1571 new->ffs_filearray = NULL;
1572 new->ffs_filearray_size = 0;
1573 new->ffs_filearray_cur = 0;
1574 new->ffs_stage = 0;
1575 new->ffs_level = level;
1576 new->ffs_star_star_empty = star_star_empty;
1577
1578 // the following saves NULL pointer checks in vim_findfile
1579 if (fix_part == NULL)
John Marriottd6e3c902025-02-18 20:45:48 +01001580 {
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001581 fix_part = (char_u *)"";
John Marriottd6e3c902025-02-18 20:45:48 +01001582 fix_partlen = 0;
1583 }
1584 new->ffs_fix_path.string = vim_strnsave(fix_part, fix_partlen);
1585 new->ffs_fix_path.length = fix_partlen;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001586
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001587 if (wc_part == NULL)
John Marriottd6e3c902025-02-18 20:45:48 +01001588 {
1589 wc_part = (char_u *)"";
1590 wc_partlen = 0;
1591 }
1592 new->ffs_wc_path.string = vim_strnsave(wc_part, wc_partlen);
1593 new->ffs_wc_path.length = wc_partlen;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001594
John Marriottd6e3c902025-02-18 20:45:48 +01001595 if (new->ffs_fix_path.string == NULL || new->ffs_wc_path.string == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001596 {
1597 ff_free_stack_element(new);
1598 new = NULL;
1599 }
1600
1601 return new;
1602}
1603
1604/*
1605 * Push a dir on the directory stack.
1606 */
1607 static void
1608ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)
1609{
1610 // check for NULL pointer, not to return an error to the user, but
1611 // to prevent a crash
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001612 if (stack_ptr == NULL)
1613 return;
1614
1615 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
1616 search_ctx->ffsc_stack_ptr = stack_ptr;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001617}
1618
1619/*
1620 * Pop a dir from the directory stack.
1621 * Returns NULL if stack is empty.
1622 */
1623 static ff_stack_T *
1624ff_pop(ff_search_ctx_T *search_ctx)
1625{
1626 ff_stack_T *sptr;
1627
1628 sptr = search_ctx->ffsc_stack_ptr;
1629 if (search_ctx->ffsc_stack_ptr != NULL)
1630 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
1631
1632 return sptr;
1633}
1634
1635/*
1636 * free the given stack element
1637 */
1638 static void
1639ff_free_stack_element(ff_stack_T *stack_ptr)
1640{
John Marriottd6e3c902025-02-18 20:45:48 +01001641 // VIM_CLEAR_STRING handles possible NULL pointers
1642 VIM_CLEAR_STRING(stack_ptr->ffs_fix_path);
1643 VIM_CLEAR_STRING(stack_ptr->ffs_wc_path);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001644
1645 if (stack_ptr->ffs_filearray != NULL)
1646 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
1647
1648 vim_free(stack_ptr);
1649}
1650
1651/*
1652 * Clear the search context, but NOT the visited list.
1653 */
1654 static void
1655ff_clear(ff_search_ctx_T *search_ctx)
1656{
1657 ff_stack_T *sptr;
1658
1659 // clear up stack
1660 while ((sptr = ff_pop(search_ctx)) != NULL)
1661 ff_free_stack_element(sptr);
1662
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001663 if (search_ctx->ffsc_stopdirs_v != NULL)
1664 {
1665 int i = 0;
1666
John Marriottd6e3c902025-02-18 20:45:48 +01001667 while (search_ctx->ffsc_stopdirs_v[i].string != NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001668 {
John Marriottd6e3c902025-02-18 20:45:48 +01001669 vim_free(search_ctx->ffsc_stopdirs_v[i].string);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001670 i++;
1671 }
John Marriottd6e3c902025-02-18 20:45:48 +01001672 VIM_CLEAR(search_ctx->ffsc_stopdirs_v);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001673 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001674
1675 // reset everything
John Marriottd6e3c902025-02-18 20:45:48 +01001676 VIM_CLEAR_STRING(search_ctx->ffsc_file_to_search);
1677 VIM_CLEAR_STRING(search_ctx->ffsc_start_dir);
1678 VIM_CLEAR_STRING(search_ctx->ffsc_fix_path);
1679 VIM_CLEAR_STRING(search_ctx->ffsc_wc_path);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001680 search_ctx->ffsc_level = 0;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001681}
1682
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001683/*
1684 * check if the given path is in the stopdirs
1685 * returns TRUE if yes else FALSE
1686 */
1687 static int
John Marriottd6e3c902025-02-18 20:45:48 +01001688ff_path_in_stoplist(char_u *path, int path_len, string_T *stopdirs_v)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001689{
1690 int i = 0;
1691
1692 // eat up trailing path separators, except the first
1693 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
1694 path_len--;
1695
1696 // if no path consider it as match
1697 if (path_len == 0)
1698 return TRUE;
1699
John Marriottd6e3c902025-02-18 20:45:48 +01001700 for (i = 0; stopdirs_v[i].string != NULL; i++)
zeertzjqe6ab23b2024-07-11 22:22:26 +02001701 // match for parent directory. So '/home' also matches
1702 // '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
1703 // '/home/r' would also match '/home/rks'
John Marriottd6e3c902025-02-18 20:45:48 +01001704 if (fnamencmp(stopdirs_v[i].string, path, path_len) == 0
1705 && ((int)stopdirs_v[i].length <= path_len
1706 || vim_ispathsep(stopdirs_v[i].string[path_len])))
zeertzjqe6ab23b2024-07-11 22:22:26 +02001707 return TRUE;
1708
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001709 return FALSE;
1710}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001711
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001712/*
1713 * Find the file name "ptr[len]" in the path. Also finds directory names.
1714 *
1715 * On the first call set the parameter 'first' to TRUE to initialize
1716 * the search. For repeating calls to FALSE.
1717 *
1718 * Repeating calls will return other files called 'ptr[len]' from the path.
1719 *
1720 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
1721 * don't need valid values.
1722 *
1723 * If nothing found on the first call the option FNAME_MESS will issue the
1724 * message:
1725 * 'Can't find file "<file>" in path'
1726 * On repeating calls:
1727 * 'No more file "<file>" found in path'
1728 *
1729 * options:
1730 * FNAME_MESS give error message when not found
1731 *
1732 * Uses NameBuff[]!
1733 *
1734 * Returns an allocated string for the file name. NULL for error.
1735 *
1736 */
1737 char_u *
1738find_file_in_path(
1739 char_u *ptr, // file name
1740 int len, // length of file name
1741 int options,
1742 int first, // use count'th matching file name
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001743 char_u *rel_fname, // file name searching relative to
1744 char_u **file_to_find, // in/out: modified copy of file name
1745 char **search_ctx) // in/out: state of the search
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001746{
1747 return find_file_in_path_option(ptr, len, options, first,
1748 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001749 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua,
1750 file_to_find, search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001751}
1752
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001753# if defined(EXITFREE) || defined(PROTO)
1754 void
1755free_findfile(void)
1756{
John Marriottd6e3c902025-02-18 20:45:48 +01001757 VIM_CLEAR_STRING(ff_expand_buffer);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001758}
1759# endif
1760
1761/*
1762 * Find the directory name "ptr[len]" in the path.
1763 *
1764 * options:
1765 * FNAME_MESS give error message when not found
1766 * FNAME_UNESC unescape backslashes.
1767 *
1768 * Uses NameBuff[]!
1769 *
1770 * Returns an allocated string for the file name. NULL for error.
1771 */
1772 char_u *
1773find_directory_in_path(
1774 char_u *ptr, // file name
1775 int len, // length of file name
1776 int options,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001777 char_u *rel_fname, // file name searching relative to
1778 char_u **file_to_find, // in/out: modified copy of file name
1779 char **search_ctx) // in/out: state of the search
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001780{
1781 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001782 FINDFILE_DIR, rel_fname, (char_u *)"",
1783 file_to_find, search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001784}
1785
1786 char_u *
1787find_file_in_path_option(
1788 char_u *ptr, // file name
1789 int len, // length of file name
1790 int options,
1791 int first, // use count'th matching file name
1792 char_u *path_option, // p_path or p_cdpath
1793 int find_what, // FINDFILE_FILE, _DIR or _BOTH
1794 char_u *rel_fname, // file name we are looking relative to.
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001795 char_u *suffixes, // list of suffixes, 'suffixesadd' option
1796 char_u **file_to_find, // in/out: modified copy of file name
1797 char **search_ctx_arg) // in/out: state of the search
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001798{
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001799 ff_search_ctx_T **search_ctx = (ff_search_ctx_T **)search_ctx_arg;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001800 static char_u *dir;
1801 static int did_findfile_init = FALSE;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001802 char_u *file_name = NULL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001803 int rel_to_curdir;
1804# ifdef AMIGA
1805 struct Process *proc = (struct Process *)FindTask(0L);
1806 APTR save_winptr = proc->pr_WindowPtr;
1807
1808 // Avoid a requester here for a volume that doesn't exist.
1809 proc->pr_WindowPtr = (APTR)-1L;
1810# endif
John Marriottd6e3c902025-02-18 20:45:48 +01001811 static size_t file_to_findlen = 0;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001812
1813 if (first == TRUE)
1814 {
John Marriottd6e3c902025-02-18 20:45:48 +01001815 char_u save_char;
1816
Bram Moolenaare015d992021-11-17 19:01:53 +00001817 if (len == 0)
1818 return NULL;
1819
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001820 // copy file name into NameBuff, expanding environment variables
1821 save_char = ptr[len];
1822 ptr[len] = NUL;
1823 expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
1824 ptr[len] = save_char;
1825
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001826 vim_free(*file_to_find);
John Marriottd6e3c902025-02-18 20:45:48 +01001827 file_to_findlen = STRLEN(NameBuff);
1828 *file_to_find = vim_strnsave(NameBuff, file_to_findlen);
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001829 if (*file_to_find == NULL) // out of memory
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001830 {
1831 file_name = NULL;
John Marriottd6e3c902025-02-18 20:45:48 +01001832 file_to_findlen = 0;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001833 goto theend;
1834 }
1835 if (options & FNAME_UNESC)
1836 {
1837 // Change all "\ " to " ".
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001838 for (ptr = *file_to_find; *ptr != NUL; ++ptr)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001839 if (ptr[0] == '\\' && ptr[1] == ' ')
John Marriottd6e3c902025-02-18 20:45:48 +01001840 {
1841 mch_memmove(ptr, ptr + 1,
1842 (size_t)((*file_to_find + file_to_findlen) - (ptr + 1)) + 1);
1843 --file_to_findlen;
1844 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001845 }
1846 }
1847
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001848 rel_to_curdir = ((*file_to_find)[0] == '.'
1849 && ((*file_to_find)[1] == NUL
1850 || vim_ispathsep((*file_to_find)[1])
1851 || ((*file_to_find)[1] == '.'
1852 && ((*file_to_find)[2] == NUL
1853 || vim_ispathsep((*file_to_find)[2])))));
1854 if (vim_isAbsName(*file_to_find)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001855 // "..", "../path", "." and "./path": don't use the path_option
1856 || rel_to_curdir
1857# if defined(MSWIN)
1858 // handle "\tmp" as absolute path
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001859 || vim_ispathsep((*file_to_find)[0])
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001860 // handle "c:name" as absolute path
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001861 || ((*file_to_find)[0] != NUL && (*file_to_find)[1] == ':')
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001862# endif
1863# ifdef AMIGA
1864 // handle ":tmp" as absolute path
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001865 || (*file_to_find)[0] == ':'
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001866# endif
1867 )
1868 {
1869 /*
1870 * Absolute path, no need to use "path_option".
1871 * If this is not a first call, return NULL. We already returned a
1872 * filename on the first call.
1873 */
1874 if (first == TRUE)
1875 {
1876 int l;
zeertzjqbf595ae2025-02-22 09:13:17 +01001877 int NameBufflen;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001878 int run;
John Marriottd6e3c902025-02-18 20:45:48 +01001879 size_t rel_fnamelen = 0;
1880 char_u *suffix;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001881
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001882 if (path_with_url(*file_to_find))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001883 {
John Marriottd6e3c902025-02-18 20:45:48 +01001884 file_name = vim_strnsave(*file_to_find, file_to_findlen);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001885 goto theend;
1886 }
1887
John Marriottd6e3c902025-02-18 20:45:48 +01001888 if (rel_fname != NULL)
1889 rel_fnamelen = STRLEN(rel_fname);
1890
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001891 // When FNAME_REL flag given first use the directory of the file.
1892 // Otherwise or when this fails use the current directory.
1893 for (run = 1; run <= 2; ++run)
1894 {
John Marriottd6e3c902025-02-18 20:45:48 +01001895 l = (int)file_to_findlen;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001896 if (run == 1
1897 && rel_to_curdir
1898 && (options & FNAME_REL)
1899 && rel_fname != NULL
John Marriottd6e3c902025-02-18 20:45:48 +01001900 && rel_fnamelen + l < MAXPATHL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001901 {
John Marriottd6e3c902025-02-18 20:45:48 +01001902 l = vim_snprintf(
1903 (char *)NameBuff,
1904 MAXPATHL,
1905 "%.*s%s",
1906 (int)(gettail(rel_fname) - rel_fname),
1907 rel_fname,
1908 *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001909 }
1910 else
1911 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001912 STRCPY(NameBuff, *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001913 run = 2;
1914 }
1915
1916 // When the file doesn't exist, try adding parts of
1917 // 'suffixesadd'.
zeertzjqbf595ae2025-02-22 09:13:17 +01001918 NameBufflen = l;
John Marriottd6e3c902025-02-18 20:45:48 +01001919 suffix = suffixes;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001920 for (;;)
1921 {
1922 if (mch_getperm(NameBuff) >= 0
1923 && (find_what == FINDFILE_BOTH
1924 || ((find_what == FINDFILE_DIR)
1925 == mch_isdir(NameBuff))))
1926 {
zeertzjqbf595ae2025-02-22 09:13:17 +01001927 file_name = vim_strnsave(NameBuff, NameBufflen);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001928 goto theend;
1929 }
John Marriottd6e3c902025-02-18 20:45:48 +01001930 if (*suffix == NUL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001931 break;
zeertzjqbf595ae2025-02-22 09:13:17 +01001932 NameBufflen = l + copy_option_part(&suffix, NameBuff + l,
1933 MAXPATHL - l, ",");
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001934 }
1935 }
1936 }
1937 }
1938 else
1939 {
1940 /*
1941 * Loop over all paths in the 'path' or 'cdpath' option.
1942 * When "first" is set, first setup to the start of the option.
1943 * Otherwise continue to find the next match.
1944 */
1945 if (first == TRUE)
1946 {
1947 // vim_findfile_free_visited can handle a possible NULL pointer
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001948 vim_findfile_free_visited(*search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001949 dir = path_option;
1950 did_findfile_init = FALSE;
1951 }
1952
1953 for (;;)
1954 {
1955 if (did_findfile_init)
1956 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001957 file_name = vim_findfile(*search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001958 if (file_name != NULL)
1959 break;
1960
1961 did_findfile_init = FALSE;
1962 }
1963 else
1964 {
John Marriottd6e3c902025-02-18 20:45:48 +01001965 char_u *buf;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001966 char_u *r_ptr;
1967
1968 if (dir == NULL || *dir == NUL)
1969 {
1970 // We searched all paths of the option, now we can
1971 // free the search context.
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001972 vim_findfile_cleanup(*search_ctx);
1973 *search_ctx = NULL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001974 break;
1975 }
1976
Bram Moolenaar51e14382019-05-25 20:21:28 +02001977 if ((buf = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001978 break;
1979
1980 // copy next path
John Marriottd6e3c902025-02-18 20:45:48 +01001981 buf[0] = NUL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001982 copy_option_part(&dir, buf, MAXPATHL, " ,");
1983
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001984 // get the stopdir string
1985 r_ptr = vim_findfile_stopdir(buf);
John Marriottd6e3c902025-02-18 20:45:48 +01001986 *search_ctx = vim_findfile_init(buf, *file_to_find, file_to_findlen,
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001987 r_ptr, 100, FALSE, find_what,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001988 *search_ctx, FALSE, rel_fname);
1989 if (*search_ctx != NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001990 did_findfile_init = TRUE;
1991 vim_free(buf);
1992 }
1993 }
1994 }
1995 if (file_name == NULL && (options & FNAME_MESS))
1996 {
1997 if (first == TRUE)
1998 {
1999 if (find_what == FINDFILE_DIR)
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002000 semsg(_(e_cant_find_directory_str_in_cdpath), *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002001 else
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002002 semsg(_(e_cant_find_file_str_in_path), *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002003 }
2004 else
2005 {
2006 if (find_what == FINDFILE_DIR)
Bram Moolenaareaaac012022-01-02 17:00:40 +00002007 semsg(_(e_no_more_directory_str_found_in_cdpath),
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002008 *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002009 else
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002010 semsg(_(e_no_more_file_str_found_in_path), *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002011 }
2012 }
2013
2014theend:
2015# ifdef AMIGA
2016 proc->pr_WindowPtr = save_winptr;
2017# endif
2018 return file_name;
2019}
2020
2021/*
2022 * Get the file name at the cursor.
2023 * If Visual mode is active, use the selected text if it's in one line.
2024 * Returns the name in allocated memory, NULL for failure.
2025 */
2026 char_u *
2027grab_file_name(long count, linenr_T *file_lnum)
2028{
2029 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
2030
2031 if (VIsual_active)
2032 {
2033 int len;
2034 char_u *ptr;
2035
2036 if (get_visual_text(NULL, &ptr, &len) == FAIL)
2037 return NULL;
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +02002038 // Only recognize ":123" here
Keith Thompson184f71c2024-01-04 21:19:04 +01002039 if (file_lnum != NULL && ptr[len] == ':' && SAFE_isdigit(ptr[len + 1]))
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +02002040 {
2041 char_u *p = ptr + len + 1;
2042
2043 *file_lnum = getdigits(&p);
2044 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002045 return find_file_name_in_path(ptr, len, options,
2046 count, curbuf->b_ffname);
2047 }
2048 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
2049}
2050
2051/*
2052 * Return the file name under or after the cursor.
2053 *
2054 * The 'path' option is searched if the file name is not absolute.
2055 * The string returned has been alloc'ed and should be freed by the caller.
2056 * NULL is returned if the file name or file is not found.
2057 *
2058 * options:
2059 * FNAME_MESS give error messages
2060 * FNAME_EXP expand to path
2061 * FNAME_HYP check for hypertext link
2062 * FNAME_INCL apply "includeexpr"
2063 */
2064 char_u *
2065file_name_at_cursor(int options, long count, linenr_T *file_lnum)
2066{
2067 return file_name_in_line(ml_get_curline(),
2068 curwin->w_cursor.col, options, count, curbuf->b_ffname,
2069 file_lnum);
2070}
2071
2072/*
2073 * Return the name of the file under or after ptr[col].
2074 * Otherwise like file_name_at_cursor().
2075 */
2076 char_u *
2077file_name_in_line(
2078 char_u *line,
2079 int col,
2080 int options,
2081 long count,
2082 char_u *rel_fname, // file we are searching relative to
2083 linenr_T *file_lnum) // line number after the file name
2084{
2085 char_u *ptr;
2086 int len;
2087 int in_type = TRUE;
2088 int is_url = FALSE;
2089
2090 /*
2091 * search forward for what could be the start of a file name
2092 */
2093 ptr = line + col;
2094 while (*ptr != NUL && !vim_isfilec(*ptr))
2095 MB_PTR_ADV(ptr);
2096 if (*ptr == NUL) // nothing found
2097 {
2098 if (options & FNAME_MESS)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002099 emsg(_(e_no_file_name_under_cursor));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002100 return NULL;
2101 }
2102
2103 /*
2104 * Search backward for first char of the file name.
2105 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
2106 */
2107 while (ptr > line)
2108 {
2109 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
2110 ptr -= len + 1;
2111 else if (vim_isfilec(ptr[-1])
2112 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
2113 --ptr;
2114 else
2115 break;
2116 }
2117
2118 /*
2119 * Search forward for the last char of the file name.
2120 * Also allow "://" when ':' is not in 'isfname'.
2121 */
2122 len = 0;
2123 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar747f1102022-09-18 13:06:41 +01002124 || ((options & FNAME_HYP) && path_is_url(ptr + len))
2125 || (is_url && vim_strchr((char_u *)":?&=", ptr[len]) != NULL))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002126 {
Bram Moolenaar747f1102022-09-18 13:06:41 +01002127 // After type:// we also include :, ?, & and = as valid characters, so
2128 // that http://google.com:8080?q=this&that=ok works.
2129 if ((ptr[len] >= 'A' && ptr[len] <= 'Z')
2130 || (ptr[len] >= 'a' && ptr[len] <= 'z'))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002131 {
2132 if (in_type && path_is_url(ptr + len + 1))
2133 is_url = TRUE;
2134 }
2135 else
2136 in_type = FALSE;
2137
2138 if (ptr[len] == '\\')
2139 // Skip over the "\" in "\ ".
2140 ++len;
2141 if (has_mbyte)
2142 len += (*mb_ptr2len)(ptr + len);
2143 else
2144 ++len;
2145 }
2146
2147 /*
2148 * If there is trailing punctuation, remove it.
2149 * But don't remove "..", could be a directory name.
2150 */
2151 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
2152 && ptr[len - 2] != '.')
2153 --len;
2154
2155 if (file_lnum != NULL)
2156 {
John Marriottd6e3c902025-02-18 20:45:48 +01002157 char_u *p;
2158 char *match_text = " line "; // english
2159 size_t match_textlen = 6;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002160
Bram Moolenaar64e74c92019-12-22 15:38:06 +01002161 // Get the number after the file name and a separator character.
2162 // Also accept " line 999" with and without the same translation as
2163 // used in last_set_msg().
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002164 p = ptr + len;
John Marriottd6e3c902025-02-18 20:45:48 +01002165 if (STRNCMP(p, match_text, match_textlen) == 0)
2166 p += match_textlen;
Bram Moolenaar64e74c92019-12-22 15:38:06 +01002167 else
John Marriottd6e3c902025-02-18 20:45:48 +01002168 {
2169 // no match with english, try localized
2170 match_text = _(line_msg);
2171 match_textlen = STRLEN(match_text);
2172
2173 if (STRNCMP(p, match_text, match_textlen) == 0)
2174 p += match_textlen;
2175 else
2176 p = skipwhite(p);
2177 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002178 if (*p != NUL)
2179 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002180 if (!SAFE_isdigit(*p))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002181 ++p; // skip the separator
2182 p = skipwhite(p);
Keith Thompson184f71c2024-01-04 21:19:04 +01002183 if (SAFE_isdigit(*p))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002184 *file_lnum = (int)getdigits(&p);
2185 }
2186 }
2187
2188 return find_file_name_in_path(ptr, len, options, count, rel_fname);
2189}
2190
2191# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2192 static char_u *
2193eval_includeexpr(char_u *ptr, int len)
2194{
2195 char_u *res;
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002196 sctx_T save_sctx = current_sctx;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002197
2198 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002199 current_sctx = curbuf->b_p_script_ctx[BV_INEX];
2200
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002201 res = eval_to_string_safe(curbuf->b_p_inex,
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002202 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL),
2203 TRUE, TRUE);
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002204
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002205 set_vim_var_string(VV_FNAME, NULL, 0);
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002206 current_sctx = save_sctx;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002207 return res;
2208}
2209# endif
2210
2211/*
2212 * Return the name of the file ptr[len] in 'path'.
2213 * Otherwise like file_name_at_cursor().
2214 */
2215 char_u *
2216find_file_name_in_path(
2217 char_u *ptr,
2218 int len,
2219 int options,
2220 long count,
2221 char_u *rel_fname) // file we are searching relative to
2222{
2223 char_u *file_name;
2224 int c;
2225# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2226 char_u *tofree = NULL;
Bram Moolenaar615ddd52021-11-17 18:00:31 +00002227# endif
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002228
Bram Moolenaar615ddd52021-11-17 18:00:31 +00002229 if (len == 0)
2230 return NULL;
2231
2232# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002233 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
2234 {
2235 tofree = eval_includeexpr(ptr, len);
2236 if (tofree != NULL)
2237 {
2238 ptr = tofree;
2239 len = (int)STRLEN(ptr);
2240 }
2241 }
2242# endif
2243
2244 if (options & FNAME_EXP)
2245 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002246 char_u *file_to_find = NULL;
2247 char *search_ctx = NULL;
2248
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002249 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002250 TRUE, rel_fname, &file_to_find, &search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002251
2252# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2253 /*
2254 * If the file could not be found in a normal way, try applying
2255 * 'includeexpr' (unless done already).
2256 */
2257 if (file_name == NULL
2258 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
2259 {
2260 tofree = eval_includeexpr(ptr, len);
2261 if (tofree != NULL)
2262 {
2263 ptr = tofree;
2264 len = (int)STRLEN(ptr);
2265 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002266 TRUE, rel_fname, &file_to_find, &search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002267 }
2268 }
2269# endif
2270 if (file_name == NULL && (options & FNAME_MESS))
2271 {
2272 c = ptr[len];
2273 ptr[len] = NUL;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002274 semsg(_(e_cant_find_file_str_in_path_2), ptr);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002275 ptr[len] = c;
2276 }
2277
2278 // Repeat finding the file "count" times. This matters when it
2279 // appears several times in the path.
2280 while (file_name != NULL && --count > 0)
2281 {
2282 vim_free(file_name);
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002283 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname,
2284 &file_to_find, &search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002285 }
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002286
2287 vim_free(file_to_find);
2288 vim_findfile_cleanup(search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002289 }
2290 else
2291 file_name = vim_strnsave(ptr, len);
2292
2293# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2294 vim_free(tofree);
2295# endif
2296
2297 return file_name;
2298}
2299
2300/*
2301 * Return the end of the directory name, on the first path
2302 * separator:
2303 * "/path/file", "/path/dir/", "/path//dir", "/file"
2304 * ^ ^ ^ ^
2305 */
2306 static char_u *
2307gettail_dir(char_u *fname)
2308{
2309 char_u *dir_end = fname;
2310 char_u *next_dir_end = fname;
2311 int look_for_sep = TRUE;
2312 char_u *p;
2313
2314 for (p = fname; *p != NUL; )
2315 {
2316 if (vim_ispathsep(*p))
2317 {
2318 if (look_for_sep)
2319 {
2320 next_dir_end = p;
2321 look_for_sep = FALSE;
2322 }
2323 }
2324 else
2325 {
2326 if (!look_for_sep)
2327 dir_end = next_dir_end;
2328 look_for_sep = TRUE;
2329 }
2330 MB_PTR_ADV(p);
2331 }
2332 return dir_end;
2333}
2334
2335/*
2336 * return TRUE if 'c' is a path list separator.
2337 */
2338 int
2339vim_ispathlistsep(int c)
2340{
2341# ifdef UNIX
2342 return (c == ':');
2343# else
2344 return (c == ';'); // might not be right for every system...
2345# endif
2346}
2347
2348/*
2349 * Moves "*psep" back to the previous path separator in "path".
2350 * Returns FAIL is "*psep" ends up at the beginning of "path".
2351 */
2352 static int
2353find_previous_pathsep(char_u *path, char_u **psep)
2354{
2355 // skip the current separator
2356 if (*psep > path && vim_ispathsep(**psep))
2357 --*psep;
2358
2359 // find the previous separator
2360 while (*psep > path)
2361 {
2362 if (vim_ispathsep(**psep))
2363 return OK;
2364 MB_PTR_BACK(path, *psep);
2365 }
2366
2367 return FAIL;
2368}
2369
2370/*
2371 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
2372 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
2373 */
2374 static int
2375is_unique(char_u *maybe_unique, garray_T *gap, int i)
2376{
2377 int j;
John Marriottd6e3c902025-02-18 20:45:48 +01002378 int candidate_len = (int)STRLEN(maybe_unique);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002379 int other_path_len;
2380 char_u **other_paths = (char_u **)gap->ga_data;
2381 char_u *rival;
2382
2383 for (j = 0; j < gap->ga_len; j++)
2384 {
2385 if (j == i)
2386 continue; // don't compare it with itself
2387
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002388 other_path_len = (int)STRLEN(other_paths[j]);
2389 if (other_path_len < candidate_len)
2390 continue; // it's different when it's shorter
2391
2392 rival = other_paths[j] + other_path_len - candidate_len;
2393 if (fnamecmp(maybe_unique, rival) == 0
2394 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
2395 return FALSE; // match
2396 }
2397
2398 return TRUE; // no match found
2399}
2400
2401/*
2402 * Split the 'path' option into an array of strings in garray_T. Relative
2403 * paths are expanded to their equivalent fullpath. This includes the "."
2404 * (relative to current buffer directory) and empty path (relative to current
2405 * directory) notations.
2406 *
2407 * TODO: handle upward search (;) and path limiter (**N) notations by
2408 * expanding each into their equivalent path(s).
2409 */
2410 static void
LemonBoya20bf692024-07-11 22:35:53 +02002411expand_path_option(
2412 char_u *curdir,
2413 char_u *path_option, // p_path or p_cdpath
2414 garray_T *gap)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002415{
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002416 char_u *buf;
John Marriottd6e3c902025-02-18 20:45:48 +01002417 size_t buflen;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002418 char_u *p;
John Marriottd6e3c902025-02-18 20:45:48 +01002419 size_t curdirlen = 0;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002420
Bram Moolenaar51e14382019-05-25 20:21:28 +02002421 if ((buf = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002422 return;
2423
2424 while (*path_option != NUL)
2425 {
John Marriottd6e3c902025-02-18 20:45:48 +01002426 buflen = copy_option_part(&path_option, buf, MAXPATHL, " ,");
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002427
2428 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
2429 {
John Marriottd6e3c902025-02-18 20:45:48 +01002430 size_t plen;
2431
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002432 // Relative to current buffer:
2433 // "/path/file" + "." -> "/path/"
2434 // "/path/file" + "./subdir" -> "/path/subdir"
2435 if (curbuf->b_ffname == NULL)
2436 continue;
2437 p = gettail(curbuf->b_ffname);
John Marriottd6e3c902025-02-18 20:45:48 +01002438 plen = (size_t)(p - curbuf->b_ffname);
2439 if (plen + buflen >= MAXPATHL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002440 continue;
2441 if (buf[1] == NUL)
John Marriottd6e3c902025-02-18 20:45:48 +01002442 buf[plen] = NUL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002443 else
John Marriottd6e3c902025-02-18 20:45:48 +01002444 mch_memmove(buf + plen, buf + 2, (buflen - 2) + 1); // +1 for NUL
2445 mch_memmove(buf, curbuf->b_ffname, plen);
2446 buflen = simplify_filename(buf);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002447 }
2448 else if (buf[0] == NUL)
John Marriottd6e3c902025-02-18 20:45:48 +01002449 {
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002450 // relative to current directory
2451 STRCPY(buf, curdir);
John Marriottd6e3c902025-02-18 20:45:48 +01002452 if (curdirlen == 0)
2453 curdirlen = STRLEN(curdir);
2454 buflen = curdirlen;
2455 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002456 else if (path_with_url(buf))
2457 // URL can't be used here
2458 continue;
2459 else if (!mch_isFullName(buf))
2460 {
2461 // Expand relative path to their full path equivalent
John Marriottd6e3c902025-02-18 20:45:48 +01002462 if (curdirlen == 0)
2463 curdirlen = STRLEN(curdir);
2464 if (curdirlen + buflen + 3 > MAXPATHL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002465 continue;
John Marriottd6e3c902025-02-18 20:45:48 +01002466
2467 mch_memmove(buf + curdirlen + 1, buf, buflen + 1); // +1 for NUL
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002468 STRCPY(buf, curdir);
John Marriottd6e3c902025-02-18 20:45:48 +01002469 buf[curdirlen] = PATHSEP;
2470 buflen = simplify_filename(buf);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002471 }
2472
2473 if (ga_grow(gap, 1) == FAIL)
2474 break;
2475
2476# if defined(MSWIN)
2477 // Avoid the path ending in a backslash, it fails when a comma is
2478 // appended.
John Marriottd6e3c902025-02-18 20:45:48 +01002479 if (buf[buflen - 1] == '\\')
2480 buf[buflen - 1] = '/';
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002481# endif
2482
John Marriottd6e3c902025-02-18 20:45:48 +01002483 p = vim_strnsave(buf, buflen);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002484 if (p == NULL)
2485 break;
2486 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
2487 }
2488
2489 vim_free(buf);
2490}
2491
2492/*
2493 * Returns a pointer to the file or directory name in "fname" that matches the
2494 * longest path in "ga"p, or NULL if there is no match. For example:
2495 *
2496 * path: /foo/bar/baz
2497 * fname: /foo/bar/baz/quux.txt
2498 * returns: ^this
2499 */
2500 static char_u *
2501get_path_cutoff(char_u *fname, garray_T *gap)
2502{
2503 int i;
2504 int maxlen = 0;
2505 char_u **path_part = (char_u **)gap->ga_data;
2506 char_u *cutoff = NULL;
2507
2508 for (i = 0; i < gap->ga_len; i++)
2509 {
2510 int j = 0;
2511
2512 while ((fname[j] == path_part[i][j]
2513# if defined(MSWIN)
2514 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
2515# endif
2516 ) && fname[j] != NUL && path_part[i][j] != NUL)
2517 j++;
2518 if (j > maxlen)
2519 {
2520 maxlen = j;
2521 cutoff = &fname[j];
2522 }
2523 }
2524
2525 // skip to the file or directory name
2526 if (cutoff != NULL)
2527 while (vim_ispathsep(*cutoff))
2528 MB_PTR_ADV(cutoff);
2529
2530 return cutoff;
2531}
2532
2533/*
2534 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
2535 * that they are unique with respect to each other while conserving the part
2536 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
2537 */
2538 void
LemonBoya20bf692024-07-11 22:35:53 +02002539uniquefy_paths(
2540 garray_T *gap,
2541 char_u *pattern,
2542 char_u *path_option) // p_path or p_cdpath
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002543{
2544 int i;
2545 int len;
2546 char_u **fnames = (char_u **)gap->ga_data;
2547 int sort_again = FALSE;
2548 char_u *pat;
2549 char_u *file_pattern;
2550 char_u *curdir;
2551 regmatch_T regmatch;
2552 garray_T path_ga;
2553 char_u **in_curdir = NULL;
2554 char_u *short_name;
2555
2556 remove_duplicates(gap);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002557 ga_init2(&path_ga, sizeof(char_u *), 1);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002558
2559 /*
2560 * We need to prepend a '*' at the beginning of file_pattern so that the
2561 * regex matches anywhere in the path. FIXME: is this valid for all
2562 * possible patterns?
2563 */
2564 len = (int)STRLEN(pattern);
2565 file_pattern = alloc(len + 2);
2566 if (file_pattern == NULL)
2567 return;
2568 file_pattern[0] = '*';
John Marriottd6e3c902025-02-18 20:45:48 +01002569 STRCPY(file_pattern + 1, pattern);
Christian Brabandt1a31c432024-10-06 16:34:20 +02002570 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, FALSE);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002571 vim_free(file_pattern);
2572 if (pat == NULL)
2573 return;
2574
2575 regmatch.rm_ic = TRUE; // always ignore case
2576 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2577 vim_free(pat);
2578 if (regmatch.regprog == NULL)
2579 return;
2580
Bram Moolenaar51e14382019-05-25 20:21:28 +02002581 if ((curdir = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002582 goto theend;
2583 mch_dirname(curdir, MAXPATHL);
LemonBoya20bf692024-07-11 22:35:53 +02002584 expand_path_option(curdir, path_option, &path_ga);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002585
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002586 in_curdir = ALLOC_CLEAR_MULT(char_u *, gap->ga_len);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002587 if (in_curdir == NULL)
2588 goto theend;
2589
2590 for (i = 0; i < gap->ga_len && !got_int; i++)
2591 {
2592 char_u *path = fnames[i];
2593 int is_in_curdir;
2594 char_u *dir_end = gettail_dir(path);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002595 char_u *path_cutoff;
2596
2597 len = (int)STRLEN(path);
2598 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
2599 && curdir[dir_end - path] == NUL;
2600 if (is_in_curdir)
John Marriottd6e3c902025-02-18 20:45:48 +01002601 in_curdir[i] = vim_strnsave(path, len);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002602
2603 // Shorten the filename while maintaining its uniqueness
2604 path_cutoff = get_path_cutoff(path, &path_ga);
2605
2606 // Don't assume all files can be reached without path when search
2607 // pattern starts with star star slash, so only remove path_cutoff
2608 // when possible.
2609 if (pattern[0] == '*' && pattern[1] == '*'
2610 && vim_ispathsep_nocolon(pattern[2])
2611 && path_cutoff != NULL
2612 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
2613 && is_unique(path_cutoff, gap, i))
2614 {
2615 sort_again = TRUE;
2616 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
2617 }
2618 else
2619 {
2620 // Here all files can be reached without path, so get shortest
2621 // unique path. We start at the end of the path.
John Marriottd6e3c902025-02-18 20:45:48 +01002622 char_u *pathsep_p = path + len - 1;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002623
2624 while (find_previous_pathsep(path, &pathsep_p))
John Marriottd6e3c902025-02-18 20:45:48 +01002625 {
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002626 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
2627 && is_unique(pathsep_p + 1, gap, i)
2628 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
2629 {
2630 sort_again = TRUE;
John Marriottd6e3c902025-02-18 20:45:48 +01002631 mch_memmove(path, pathsep_p + 1,
2632 (size_t)((path + len) - (pathsep_p + 1)) + 1); // +1 for NUL
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002633 break;
2634 }
John Marriottd6e3c902025-02-18 20:45:48 +01002635 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002636 }
2637
2638 if (mch_isFullName(path))
2639 {
2640 /*
2641 * Last resort: shorten relative to curdir if possible.
2642 * 'possible' means:
2643 * 1. It is under the current directory.
2644 * 2. The result is actually shorter than the original.
2645 *
2646 * Before curdir After
2647 * /foo/bar/file.txt /foo/bar ./file.txt
2648 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
2649 * /file.txt / /file.txt
2650 * c:\file.txt c:\ .\file.txt
2651 */
2652 short_name = shorten_fname(path, curdir);
2653 if (short_name != NULL && short_name > path + 1
2654# if defined(MSWIN)
2655 // On windows,
2656 // shorten_fname("c:\a\a.txt", "c:\a\b")
2657 // returns "\a\a.txt", which is not really the short
2658 // name, hence:
2659 && !vim_ispathsep(*short_name)
2660# endif
2661 )
2662 {
John Marriottd6e3c902025-02-18 20:45:48 +01002663 vim_snprintf((char *)path, MAXPATHL, ".%s%s", PATHSEPSTR, short_name);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002664 }
2665 }
2666 ui_breakcheck();
2667 }
2668
2669 // Shorten filenames in /in/current/directory/{filename}
2670 for (i = 0; i < gap->ga_len && !got_int; i++)
2671 {
John Marriottd6e3c902025-02-18 20:45:48 +01002672 size_t rel_pathsize;
2673 char_u *rel_path;
2674 char_u *path = in_curdir[i];
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002675
2676 if (path == NULL)
2677 continue;
2678
2679 // If the {filename} is not unique, change it to ./{filename}.
2680 // Else reduce it to {filename}
2681 short_name = shorten_fname(path, curdir);
2682 if (short_name == NULL)
2683 short_name = path;
2684 if (is_unique(short_name, gap, i))
2685 {
2686 STRCPY(fnames[i], short_name);
2687 continue;
2688 }
2689
John Marriottd6e3c902025-02-18 20:45:48 +01002690 rel_pathsize = 1 + STRLEN_LITERAL(PATHSEPSTR) + STRLEN(short_name) + 1;
2691 rel_path = alloc(rel_pathsize);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002692 if (rel_path == NULL)
2693 goto theend;
John Marriottd6e3c902025-02-18 20:45:48 +01002694
2695 vim_snprintf((char *)rel_path, rel_pathsize, ".%s%s", PATHSEPSTR, short_name);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002696
2697 vim_free(fnames[i]);
2698 fnames[i] = rel_path;
2699 sort_again = TRUE;
2700 ui_breakcheck();
2701 }
2702
2703theend:
2704 vim_free(curdir);
2705 if (in_curdir != NULL)
2706 {
2707 for (i = 0; i < gap->ga_len; i++)
2708 vim_free(in_curdir[i]);
2709 vim_free(in_curdir);
2710 }
2711 ga_clear_strings(&path_ga);
2712 vim_regfree(regmatch.regprog);
2713
2714 if (sort_again)
2715 remove_duplicates(gap);
2716}
2717
2718/*
2719 * Calls globpath() with 'path' values for the given pattern and stores the
2720 * result in "gap".
2721 * Returns the total number of matches.
2722 */
2723 int
2724expand_in_path(
2725 garray_T *gap,
2726 char_u *pattern,
2727 int flags) // EW_* flags
2728{
2729 char_u *curdir;
2730 garray_T path_ga;
2731 char_u *paths = NULL;
2732 int glob_flags = 0;
LemonBoya20bf692024-07-11 22:35:53 +02002733 char_u *path_option = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002734
Bram Moolenaar964b3742019-05-24 18:54:09 +02002735 if ((curdir = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002736 return 0;
2737 mch_dirname(curdir, MAXPATHL);
2738
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002739 ga_init2(&path_ga, sizeof(char_u *), 1);
LemonBoya20bf692024-07-11 22:35:53 +02002740 if (flags & EW_CDPATH)
2741 expand_path_option(curdir, p_cdpath, &path_ga);
2742 else
2743 expand_path_option(curdir, path_option, &path_ga);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002744 vim_free(curdir);
2745 if (path_ga.ga_len == 0)
2746 return 0;
2747
2748 paths = ga_concat_strings(&path_ga, ",");
2749 ga_clear_strings(&path_ga);
2750 if (paths == NULL)
2751 return 0;
2752
2753 if (flags & EW_ICASE)
2754 glob_flags |= WILD_ICASE;
2755 if (flags & EW_ADDSLASH)
2756 glob_flags |= WILD_ADD_SLASH;
LemonBoya20bf692024-07-11 22:35:53 +02002757 globpath(paths, pattern, gap, glob_flags, !!(flags & EW_CDPATH));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002758 vim_free(paths);
2759
2760 return gap->ga_len;
2761}
2762
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002763
2764/*
2765 * Converts a file name into a canonical form. It simplifies a file name into
2766 * its simplest form by stripping out unneeded components, if any. The
2767 * resulting file name is simplified in place and will either be the same
2768 * length as that supplied, or shorter.
2769 */
John Marriottd6e3c902025-02-18 20:45:48 +01002770 size_t
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002771simplify_filename(char_u *filename)
2772{
2773#ifndef AMIGA // Amiga doesn't have "..", it uses "/"
2774 int components = 0;
2775 char_u *p, *tail, *start;
John Marriottd6e3c902025-02-18 20:45:48 +01002776 char_u *p_end; // point to NUL at end of string "p"
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002777 int stripping_disabled = FALSE;
2778 int relative = TRUE;
2779
2780 p = filename;
2781# ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmanan6df0f272021-12-16 13:06:10 +00002782 if (p[0] != NUL && p[1] == ':') // skip "x:"
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002783 p += 2;
2784# endif
2785
2786 if (vim_ispathsep(*p))
2787 {
2788 relative = FALSE;
2789 do
2790 ++p;
2791 while (vim_ispathsep(*p));
2792 }
2793 start = p; // remember start after "c:/" or "/" or "///"
John Marriottd6e3c902025-02-18 20:45:48 +01002794 p_end = p + STRLEN(p);
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02002795#ifdef UNIX
2796 // Posix says that "//path" is unchanged but "///path" is "/path".
2797 if (start > filename + 2)
2798 {
John Marriottd6e3c902025-02-18 20:45:48 +01002799 mch_memmove(filename + 1, p, (size_t)(p_end - p) + 1); // +1 for NUL
2800 p_end -= (size_t)(p - (filename + 1));
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02002801 start = p = filename + 1;
2802 }
2803#endif
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002804
2805 do
2806 {
2807 // At this point "p" is pointing to the char following a single "/"
2808 // or "p" is at the "start" of the (absolute or relative) path name.
2809# ifdef VMS
2810 // VMS allows device:[path] - don't strip the [ in directory
2811 if ((*p == '[' || *p == '<') && p > filename && p[-1] == ':')
2812 {
2813 // :[ or :< composition: vms directory component
2814 ++components;
2815 p = getnextcomp(p + 1);
2816 }
2817 // allow remote calls as host"user passwd"::device:[path]
2818 else if (p[0] == ':' && p[1] == ':' && p > filename && p[-1] == '"' )
2819 {
2820 // ":: composition: vms host/passwd component
2821 ++components;
2822 p = getnextcomp(p + 2);
2823 }
2824 else
2825# endif
John Marriottd6e3c902025-02-18 20:45:48 +01002826 if (vim_ispathsep(*p))
2827 {
2828 mch_memmove(p, p + 1, (size_t)(p_end - (p + 1)) + 1); // remove duplicate "/"
2829 --p_end;
2830 }
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002831 else if (p[0] == '.' && (vim_ispathsep(p[1]) || p[1] == NUL))
2832 {
2833 if (p == start && relative)
2834 p += 1 + (p[1] != NUL); // keep single "." or leading "./"
2835 else
2836 {
2837 // Strip "./" or ".///". If we are at the end of the file name
2838 // and there is no trailing path separator, either strip "/." if
2839 // we are after "start", or strip "." if we are at the beginning
2840 // of an absolute path name .
2841 tail = p + 1;
2842 if (p[1] != NUL)
2843 while (vim_ispathsep(*tail))
2844 MB_PTR_ADV(tail);
2845 else if (p > start)
2846 --p; // strip preceding path separator
John Marriottd6e3c902025-02-18 20:45:48 +01002847
2848 mch_memmove(p, tail, (size_t)(p_end - tail) + 1);
2849 p_end -= (size_t)(tail - p);
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002850 }
2851 }
2852 else if (p[0] == '.' && p[1] == '.' &&
2853 (vim_ispathsep(p[2]) || p[2] == NUL))
2854 {
2855 // Skip to after ".." or "../" or "..///".
2856 tail = p + 2;
2857 while (vim_ispathsep(*tail))
2858 MB_PTR_ADV(tail);
2859
2860 if (components > 0) // strip one preceding component
2861 {
2862 int do_strip = FALSE;
2863 char_u saved_char;
2864 stat_T st;
2865
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002866 // Don't strip for an erroneous file name.
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002867 if (!stripping_disabled)
2868 {
2869 // If the preceding component does not exist in the file
2870 // system, we strip it. On Unix, we don't accept a symbolic
2871 // link that refers to a non-existent file.
2872 saved_char = p[-1];
2873 p[-1] = NUL;
2874# ifdef UNIX
2875 if (mch_lstat((char *)filename, &st) < 0)
2876# else
2877 if (mch_stat((char *)filename, &st) < 0)
2878# endif
2879 do_strip = TRUE;
2880 p[-1] = saved_char;
2881
2882 --p;
2883 // Skip back to after previous '/'.
2884 while (p > start && !after_pathsep(start, p))
2885 MB_PTR_BACK(start, p);
2886
2887 if (!do_strip)
2888 {
2889 // If the component exists in the file system, check
2890 // that stripping it won't change the meaning of the
2891 // file name. First get information about the
2892 // unstripped file name. This may fail if the component
2893 // to strip is not a searchable directory (but a regular
2894 // file, for instance), since the trailing "/.." cannot
2895 // be applied then. We don't strip it then since we
2896 // don't want to replace an erroneous file name by
2897 // a valid one, and we disable stripping of later
2898 // components.
2899 saved_char = *tail;
2900 *tail = NUL;
2901 if (mch_stat((char *)filename, &st) >= 0)
2902 do_strip = TRUE;
2903 else
2904 stripping_disabled = TRUE;
2905 *tail = saved_char;
2906# ifdef UNIX
2907 if (do_strip)
2908 {
2909 stat_T new_st;
2910
2911 // On Unix, the check for the unstripped file name
2912 // above works also for a symbolic link pointing to
2913 // a searchable directory. But then the parent of
2914 // the directory pointed to by the link must be the
2915 // same as the stripped file name. (The latter
2916 // exists in the file system since it is the
2917 // component's parent directory.)
2918 if (p == start && relative)
2919 (void)mch_stat(".", &new_st);
2920 else
2921 {
2922 saved_char = *p;
2923 *p = NUL;
2924 (void)mch_stat((char *)filename, &new_st);
2925 *p = saved_char;
2926 }
2927
2928 if (new_st.st_ino != st.st_ino ||
2929 new_st.st_dev != st.st_dev)
2930 {
2931 do_strip = FALSE;
2932 // We don't disable stripping of later
2933 // components since the unstripped path name is
2934 // still valid.
2935 }
2936 }
2937# endif
2938 }
2939 }
2940
2941 if (!do_strip)
2942 {
2943 // Skip the ".." or "../" and reset the counter for the
2944 // components that might be stripped later on.
2945 p = tail;
2946 components = 0;
2947 }
2948 else
2949 {
2950 // Strip previous component. If the result would get empty
2951 // and there is no trailing path separator, leave a single
2952 // "." instead. If we are at the end of the file name and
2953 // there is no trailing path separator and a preceding
2954 // component is left after stripping, strip its trailing
2955 // path separator as well.
2956 if (p == start && relative && tail[-1] == '.')
2957 {
2958 *p++ = '.';
2959 *p = NUL;
2960 }
2961 else
2962 {
2963 if (p > start && tail[-1] == '.')
2964 --p;
John Marriottd6e3c902025-02-18 20:45:48 +01002965
2966 mch_memmove(p, tail, (size_t)(p_end - tail) + 1); // strip previous component
2967 p_end -= (size_t)(tail - p);
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002968 }
2969
2970 --components;
2971 }
2972 }
2973 else if (p == start && !relative) // leading "/.." or "/../"
John Marriottd6e3c902025-02-18 20:45:48 +01002974 {
2975 mch_memmove(p, tail, (size_t)(p_end - tail) + 1); // strip ".." or "../"
2976 p_end -= (size_t)(tail - p);
2977 }
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002978 else
2979 {
2980 if (p == start + 2 && p[-2] == '.') // leading "./../"
2981 {
John Marriottd6e3c902025-02-18 20:45:48 +01002982 mch_memmove(p - 2, p, (size_t)(p_end - p) + 1); // strip leading "./"
2983 p_end -= 2;
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002984 tail -= 2;
2985 }
2986 p = tail; // skip to char after ".." or "../"
2987 }
2988 }
2989 else
2990 {
2991 ++components; // simple path component
2992 p = getnextcomp(p);
2993 }
2994 } while (*p != NUL);
2995#endif // !AMIGA
John Marriottd6e3c902025-02-18 20:45:48 +01002996
2997 return (size_t)(p_end - filename);
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002998}
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002999
3000#if defined(FEAT_EVAL) || defined(PROTO)
3001/*
3002 * "simplify()" function
3003 */
3004 void
3005f_simplify(typval_T *argvars, typval_T *rettv)
3006{
3007 char_u *p;
3008
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003009 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3010 return;
3011
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02003012 p = tv_get_string_strict(&argvars[0]);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02003013 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003014 simplify_filename(rettv->vval.v_string); // simplify in place
Bram Moolenaarb005cd82019-09-04 15:54:55 +02003015 rettv->v_type = VAR_STRING;
3016}
3017#endif // FEAT_EVAL