blob: 0f5f2dc624b47f2c82bfbf53ecafeb082b6b62b3 [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{
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +0200165 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;
170 string_T ffsc_file_to_search;
171 string_T ffsc_start_dir;
172 string_T ffsc_fix_path;
173 string_T ffsc_wc_path;
174 int ffsc_level;
175 string_T *ffsc_stopdirs_v;
176 int ffsc_find_what;
177 int ffsc_tagfile;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100178} 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;
John Marriottfff01322025-06-18 18:15:31 +02001823 file_to_findlen = expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001824 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_find = vim_strnsave(NameBuff, file_to_findlen);
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001828 if (*file_to_find == NULL) // out of memory
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001829 {
1830 file_name = NULL;
John Marriottd6e3c902025-02-18 20:45:48 +01001831 file_to_findlen = 0;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001832 goto theend;
1833 }
1834 if (options & FNAME_UNESC)
1835 {
1836 // Change all "\ " to " ".
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001837 for (ptr = *file_to_find; *ptr != NUL; ++ptr)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001838 if (ptr[0] == '\\' && ptr[1] == ' ')
John Marriottd6e3c902025-02-18 20:45:48 +01001839 {
1840 mch_memmove(ptr, ptr + 1,
1841 (size_t)((*file_to_find + file_to_findlen) - (ptr + 1)) + 1);
1842 --file_to_findlen;
1843 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001844 }
1845 }
1846
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001847 rel_to_curdir = ((*file_to_find)[0] == '.'
1848 && ((*file_to_find)[1] == NUL
1849 || vim_ispathsep((*file_to_find)[1])
1850 || ((*file_to_find)[1] == '.'
1851 && ((*file_to_find)[2] == NUL
1852 || vim_ispathsep((*file_to_find)[2])))));
1853 if (vim_isAbsName(*file_to_find)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001854 // "..", "../path", "." and "./path": don't use the path_option
1855 || rel_to_curdir
1856# if defined(MSWIN)
1857 // handle "\tmp" as absolute path
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001858 || vim_ispathsep((*file_to_find)[0])
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001859 // handle "c:name" as absolute path
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001860 || ((*file_to_find)[0] != NUL && (*file_to_find)[1] == ':')
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001861# endif
1862# ifdef AMIGA
1863 // handle ":tmp" as absolute path
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001864 || (*file_to_find)[0] == ':'
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001865# endif
1866 )
1867 {
1868 /*
1869 * Absolute path, no need to use "path_option".
1870 * If this is not a first call, return NULL. We already returned a
1871 * filename on the first call.
1872 */
1873 if (first == TRUE)
1874 {
1875 int l;
zeertzjqbf595ae2025-02-22 09:13:17 +01001876 int NameBufflen;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001877 int run;
John Marriottd6e3c902025-02-18 20:45:48 +01001878 size_t rel_fnamelen = 0;
1879 char_u *suffix;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001880
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001881 if (path_with_url(*file_to_find))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001882 {
John Marriottd6e3c902025-02-18 20:45:48 +01001883 file_name = vim_strnsave(*file_to_find, file_to_findlen);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001884 goto theend;
1885 }
1886
John Marriottd6e3c902025-02-18 20:45:48 +01001887 if (rel_fname != NULL)
1888 rel_fnamelen = STRLEN(rel_fname);
1889
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001890 // When FNAME_REL flag given first use the directory of the file.
1891 // Otherwise or when this fails use the current directory.
1892 for (run = 1; run <= 2; ++run)
1893 {
John Marriottd6e3c902025-02-18 20:45:48 +01001894 l = (int)file_to_findlen;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001895 if (run == 1
1896 && rel_to_curdir
1897 && (options & FNAME_REL)
1898 && rel_fname != NULL
John Marriottd6e3c902025-02-18 20:45:48 +01001899 && rel_fnamelen + l < MAXPATHL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001900 {
John Marriottd6e3c902025-02-18 20:45:48 +01001901 l = vim_snprintf(
1902 (char *)NameBuff,
1903 MAXPATHL,
1904 "%.*s%s",
1905 (int)(gettail(rel_fname) - rel_fname),
1906 rel_fname,
1907 *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001908 }
1909 else
1910 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001911 STRCPY(NameBuff, *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001912 run = 2;
1913 }
1914
1915 // When the file doesn't exist, try adding parts of
1916 // 'suffixesadd'.
zeertzjqbf595ae2025-02-22 09:13:17 +01001917 NameBufflen = l;
John Marriottd6e3c902025-02-18 20:45:48 +01001918 suffix = suffixes;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001919 for (;;)
1920 {
1921 if (mch_getperm(NameBuff) >= 0
1922 && (find_what == FINDFILE_BOTH
1923 || ((find_what == FINDFILE_DIR)
1924 == mch_isdir(NameBuff))))
1925 {
zeertzjqbf595ae2025-02-22 09:13:17 +01001926 file_name = vim_strnsave(NameBuff, NameBufflen);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001927 goto theend;
1928 }
John Marriottd6e3c902025-02-18 20:45:48 +01001929 if (*suffix == NUL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001930 break;
zeertzjqbf595ae2025-02-22 09:13:17 +01001931 NameBufflen = l + copy_option_part(&suffix, NameBuff + l,
1932 MAXPATHL - l, ",");
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001933 }
1934 }
1935 }
1936 }
1937 else
1938 {
1939 /*
1940 * Loop over all paths in the 'path' or 'cdpath' option.
1941 * When "first" is set, first setup to the start of the option.
1942 * Otherwise continue to find the next match.
1943 */
1944 if (first == TRUE)
1945 {
1946 // vim_findfile_free_visited can handle a possible NULL pointer
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001947 vim_findfile_free_visited(*search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001948 dir = path_option;
1949 did_findfile_init = FALSE;
1950 }
1951
1952 for (;;)
1953 {
1954 if (did_findfile_init)
1955 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001956 file_name = vim_findfile(*search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001957 if (file_name != NULL)
1958 break;
1959
1960 did_findfile_init = FALSE;
1961 }
1962 else
1963 {
John Marriottd6e3c902025-02-18 20:45:48 +01001964 char_u *buf;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001965 char_u *r_ptr;
1966
1967 if (dir == NULL || *dir == NUL)
1968 {
1969 // We searched all paths of the option, now we can
1970 // free the search context.
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001971 vim_findfile_cleanup(*search_ctx);
1972 *search_ctx = NULL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001973 break;
1974 }
1975
Bram Moolenaar51e14382019-05-25 20:21:28 +02001976 if ((buf = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001977 break;
1978
1979 // copy next path
John Marriottd6e3c902025-02-18 20:45:48 +01001980 buf[0] = NUL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001981 copy_option_part(&dir, buf, MAXPATHL, " ,");
1982
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001983 // get the stopdir string
1984 r_ptr = vim_findfile_stopdir(buf);
John Marriottd6e3c902025-02-18 20:45:48 +01001985 *search_ctx = vim_findfile_init(buf, *file_to_find, file_to_findlen,
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001986 r_ptr, 100, FALSE, find_what,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001987 *search_ctx, FALSE, rel_fname);
1988 if (*search_ctx != NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001989 did_findfile_init = TRUE;
1990 vim_free(buf);
1991 }
1992 }
1993 }
1994 if (file_name == NULL && (options & FNAME_MESS))
1995 {
1996 if (first == TRUE)
1997 {
1998 if (find_what == FINDFILE_DIR)
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001999 semsg(_(e_cant_find_directory_str_in_cdpath), *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002000 else
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002001 semsg(_(e_cant_find_file_str_in_path), *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002002 }
2003 else
2004 {
2005 if (find_what == FINDFILE_DIR)
Bram Moolenaareaaac012022-01-02 17:00:40 +00002006 semsg(_(e_no_more_directory_str_found_in_cdpath),
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002007 *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002008 else
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002009 semsg(_(e_no_more_file_str_found_in_path), *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002010 }
2011 }
2012
2013theend:
2014# ifdef AMIGA
2015 proc->pr_WindowPtr = save_winptr;
2016# endif
2017 return file_name;
2018}
2019
2020/*
2021 * Get the file name at the cursor.
2022 * If Visual mode is active, use the selected text if it's in one line.
2023 * Returns the name in allocated memory, NULL for failure.
2024 */
2025 char_u *
2026grab_file_name(long count, linenr_T *file_lnum)
2027{
2028 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
2029
2030 if (VIsual_active)
2031 {
2032 int len;
2033 char_u *ptr;
2034
2035 if (get_visual_text(NULL, &ptr, &len) == FAIL)
2036 return NULL;
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +02002037 // Only recognize ":123" here
Keith Thompson184f71c2024-01-04 21:19:04 +01002038 if (file_lnum != NULL && ptr[len] == ':' && SAFE_isdigit(ptr[len + 1]))
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +02002039 {
2040 char_u *p = ptr + len + 1;
2041
2042 *file_lnum = getdigits(&p);
2043 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002044 return find_file_name_in_path(ptr, len, options,
2045 count, curbuf->b_ffname);
2046 }
2047 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
2048}
2049
2050/*
2051 * Return the file name under or after the cursor.
2052 *
2053 * The 'path' option is searched if the file name is not absolute.
2054 * The string returned has been alloc'ed and should be freed by the caller.
2055 * NULL is returned if the file name or file is not found.
2056 *
2057 * options:
2058 * FNAME_MESS give error messages
2059 * FNAME_EXP expand to path
2060 * FNAME_HYP check for hypertext link
2061 * FNAME_INCL apply "includeexpr"
2062 */
2063 char_u *
2064file_name_at_cursor(int options, long count, linenr_T *file_lnum)
2065{
2066 return file_name_in_line(ml_get_curline(),
2067 curwin->w_cursor.col, options, count, curbuf->b_ffname,
2068 file_lnum);
2069}
2070
2071/*
2072 * Return the name of the file under or after ptr[col].
2073 * Otherwise like file_name_at_cursor().
2074 */
2075 char_u *
2076file_name_in_line(
2077 char_u *line,
2078 int col,
2079 int options,
2080 long count,
2081 char_u *rel_fname, // file we are searching relative to
2082 linenr_T *file_lnum) // line number after the file name
2083{
2084 char_u *ptr;
2085 int len;
2086 int in_type = TRUE;
2087 int is_url = FALSE;
2088
2089 /*
2090 * search forward for what could be the start of a file name
2091 */
2092 ptr = line + col;
2093 while (*ptr != NUL && !vim_isfilec(*ptr))
2094 MB_PTR_ADV(ptr);
2095 if (*ptr == NUL) // nothing found
2096 {
2097 if (options & FNAME_MESS)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002098 emsg(_(e_no_file_name_under_cursor));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002099 return NULL;
2100 }
2101
2102 /*
2103 * Search backward for first char of the file name.
2104 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
2105 */
2106 while (ptr > line)
2107 {
2108 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
2109 ptr -= len + 1;
2110 else if (vim_isfilec(ptr[-1])
2111 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
2112 --ptr;
2113 else
2114 break;
2115 }
2116
2117 /*
2118 * Search forward for the last char of the file name.
2119 * Also allow "://" when ':' is not in 'isfname'.
2120 */
2121 len = 0;
2122 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar747f1102022-09-18 13:06:41 +01002123 || ((options & FNAME_HYP) && path_is_url(ptr + len))
2124 || (is_url && vim_strchr((char_u *)":?&=", ptr[len]) != NULL))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002125 {
Bram Moolenaar747f1102022-09-18 13:06:41 +01002126 // After type:// we also include :, ?, & and = as valid characters, so
2127 // that http://google.com:8080?q=this&that=ok works.
2128 if ((ptr[len] >= 'A' && ptr[len] <= 'Z')
2129 || (ptr[len] >= 'a' && ptr[len] <= 'z'))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002130 {
2131 if (in_type && path_is_url(ptr + len + 1))
2132 is_url = TRUE;
2133 }
2134 else
2135 in_type = FALSE;
2136
2137 if (ptr[len] == '\\')
2138 // Skip over the "\" in "\ ".
2139 ++len;
2140 if (has_mbyte)
2141 len += (*mb_ptr2len)(ptr + len);
2142 else
2143 ++len;
2144 }
2145
2146 /*
2147 * If there is trailing punctuation, remove it.
2148 * But don't remove "..", could be a directory name.
2149 */
2150 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
2151 && ptr[len - 2] != '.')
2152 --len;
2153
2154 if (file_lnum != NULL)
2155 {
John Marriottd6e3c902025-02-18 20:45:48 +01002156 char_u *p;
2157 char *match_text = " line "; // english
2158 size_t match_textlen = 6;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002159
Bram Moolenaar64e74c92019-12-22 15:38:06 +01002160 // Get the number after the file name and a separator character.
2161 // Also accept " line 999" with and without the same translation as
2162 // used in last_set_msg().
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002163 p = ptr + len;
John Marriottd6e3c902025-02-18 20:45:48 +01002164 if (STRNCMP(p, match_text, match_textlen) == 0)
2165 p += match_textlen;
Bram Moolenaar64e74c92019-12-22 15:38:06 +01002166 else
John Marriottd6e3c902025-02-18 20:45:48 +01002167 {
2168 // no match with english, try localized
2169 match_text = _(line_msg);
2170 match_textlen = STRLEN(match_text);
2171
2172 if (STRNCMP(p, match_text, match_textlen) == 0)
2173 p += match_textlen;
2174 else
2175 p = skipwhite(p);
2176 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002177 if (*p != NUL)
2178 {
Keith Thompson184f71c2024-01-04 21:19:04 +01002179 if (!SAFE_isdigit(*p))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002180 ++p; // skip the separator
2181 p = skipwhite(p);
Keith Thompson184f71c2024-01-04 21:19:04 +01002182 if (SAFE_isdigit(*p))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002183 *file_lnum = (int)getdigits(&p);
2184 }
2185 }
2186
2187 return find_file_name_in_path(ptr, len, options, count, rel_fname);
2188}
2189
2190# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2191 static char_u *
2192eval_includeexpr(char_u *ptr, int len)
2193{
2194 char_u *res;
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002195 sctx_T save_sctx = current_sctx;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002196
2197 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002198 current_sctx = curbuf->b_p_script_ctx[BV_INEX];
2199
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002200 res = eval_to_string_safe(curbuf->b_p_inex,
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002201 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL),
2202 TRUE, TRUE);
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002203
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002204 set_vim_var_string(VV_FNAME, NULL, 0);
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002205 current_sctx = save_sctx;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002206 return res;
2207}
2208# endif
2209
2210/*
2211 * Return the name of the file ptr[len] in 'path'.
2212 * Otherwise like file_name_at_cursor().
2213 */
2214 char_u *
2215find_file_name_in_path(
2216 char_u *ptr,
2217 int len,
2218 int options,
2219 long count,
2220 char_u *rel_fname) // file we are searching relative to
2221{
2222 char_u *file_name;
2223 int c;
2224# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2225 char_u *tofree = NULL;
Bram Moolenaar615ddd52021-11-17 18:00:31 +00002226# endif
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002227
Bram Moolenaar615ddd52021-11-17 18:00:31 +00002228 if (len == 0)
2229 return NULL;
2230
2231# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002232 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
2233 {
2234 tofree = eval_includeexpr(ptr, len);
2235 if (tofree != NULL)
2236 {
2237 ptr = tofree;
2238 len = (int)STRLEN(ptr);
2239 }
2240 }
2241# endif
2242
2243 if (options & FNAME_EXP)
2244 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002245 char_u *file_to_find = NULL;
2246 char *search_ctx = NULL;
2247
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002248 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002249 TRUE, rel_fname, &file_to_find, &search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002250
2251# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2252 /*
2253 * If the file could not be found in a normal way, try applying
2254 * 'includeexpr' (unless done already).
2255 */
2256 if (file_name == NULL
2257 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
2258 {
2259 tofree = eval_includeexpr(ptr, len);
2260 if (tofree != NULL)
2261 {
2262 ptr = tofree;
2263 len = (int)STRLEN(ptr);
2264 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002265 TRUE, rel_fname, &file_to_find, &search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002266 }
2267 }
2268# endif
2269 if (file_name == NULL && (options & FNAME_MESS))
2270 {
2271 c = ptr[len];
2272 ptr[len] = NUL;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002273 semsg(_(e_cant_find_file_str_in_path_2), ptr);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002274 ptr[len] = c;
2275 }
2276
2277 // Repeat finding the file "count" times. This matters when it
2278 // appears several times in the path.
2279 while (file_name != NULL && --count > 0)
2280 {
2281 vim_free(file_name);
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002282 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname,
2283 &file_to_find, &search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002284 }
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002285
2286 vim_free(file_to_find);
2287 vim_findfile_cleanup(search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002288 }
2289 else
2290 file_name = vim_strnsave(ptr, len);
2291
2292# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2293 vim_free(tofree);
2294# endif
2295
2296 return file_name;
2297}
2298
2299/*
2300 * Return the end of the directory name, on the first path
2301 * separator:
2302 * "/path/file", "/path/dir/", "/path//dir", "/file"
2303 * ^ ^ ^ ^
2304 */
2305 static char_u *
2306gettail_dir(char_u *fname)
2307{
2308 char_u *dir_end = fname;
2309 char_u *next_dir_end = fname;
2310 int look_for_sep = TRUE;
2311 char_u *p;
2312
2313 for (p = fname; *p != NUL; )
2314 {
2315 if (vim_ispathsep(*p))
2316 {
2317 if (look_for_sep)
2318 {
2319 next_dir_end = p;
2320 look_for_sep = FALSE;
2321 }
2322 }
2323 else
2324 {
2325 if (!look_for_sep)
2326 dir_end = next_dir_end;
2327 look_for_sep = TRUE;
2328 }
2329 MB_PTR_ADV(p);
2330 }
2331 return dir_end;
2332}
2333
2334/*
2335 * return TRUE if 'c' is a path list separator.
2336 */
2337 int
2338vim_ispathlistsep(int c)
2339{
2340# ifdef UNIX
2341 return (c == ':');
2342# else
2343 return (c == ';'); // might not be right for every system...
2344# endif
2345}
2346
2347/*
2348 * Moves "*psep" back to the previous path separator in "path".
2349 * Returns FAIL is "*psep" ends up at the beginning of "path".
2350 */
2351 static int
2352find_previous_pathsep(char_u *path, char_u **psep)
2353{
2354 // skip the current separator
2355 if (*psep > path && vim_ispathsep(**psep))
2356 --*psep;
2357
2358 // find the previous separator
2359 while (*psep > path)
2360 {
2361 if (vim_ispathsep(**psep))
2362 return OK;
2363 MB_PTR_BACK(path, *psep);
2364 }
2365
2366 return FAIL;
2367}
2368
2369/*
2370 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
2371 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
2372 */
2373 static int
2374is_unique(char_u *maybe_unique, garray_T *gap, int i)
2375{
2376 int j;
John Marriottd6e3c902025-02-18 20:45:48 +01002377 int candidate_len = (int)STRLEN(maybe_unique);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002378 int other_path_len;
2379 char_u **other_paths = (char_u **)gap->ga_data;
2380 char_u *rival;
2381
2382 for (j = 0; j < gap->ga_len; j++)
2383 {
2384 if (j == i)
2385 continue; // don't compare it with itself
2386
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002387 other_path_len = (int)STRLEN(other_paths[j]);
2388 if (other_path_len < candidate_len)
2389 continue; // it's different when it's shorter
2390
2391 rival = other_paths[j] + other_path_len - candidate_len;
2392 if (fnamecmp(maybe_unique, rival) == 0
2393 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
2394 return FALSE; // match
2395 }
2396
2397 return TRUE; // no match found
2398}
2399
2400/*
2401 * Split the 'path' option into an array of strings in garray_T. Relative
2402 * paths are expanded to their equivalent fullpath. This includes the "."
2403 * (relative to current buffer directory) and empty path (relative to current
2404 * directory) notations.
2405 *
2406 * TODO: handle upward search (;) and path limiter (**N) notations by
2407 * expanding each into their equivalent path(s).
2408 */
2409 static void
LemonBoya20bf692024-07-11 22:35:53 +02002410expand_path_option(
2411 char_u *curdir,
2412 char_u *path_option, // p_path or p_cdpath
2413 garray_T *gap)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002414{
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002415 char_u *buf;
John Marriottd6e3c902025-02-18 20:45:48 +01002416 size_t buflen;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002417 char_u *p;
John Marriottd6e3c902025-02-18 20:45:48 +01002418 size_t curdirlen = 0;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002419
Bram Moolenaar51e14382019-05-25 20:21:28 +02002420 if ((buf = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002421 return;
2422
2423 while (*path_option != NUL)
2424 {
John Marriottd6e3c902025-02-18 20:45:48 +01002425 buflen = copy_option_part(&path_option, buf, MAXPATHL, " ,");
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002426
2427 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
2428 {
John Marriottd6e3c902025-02-18 20:45:48 +01002429 size_t plen;
2430
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002431 // Relative to current buffer:
2432 // "/path/file" + "." -> "/path/"
2433 // "/path/file" + "./subdir" -> "/path/subdir"
2434 if (curbuf->b_ffname == NULL)
2435 continue;
2436 p = gettail(curbuf->b_ffname);
John Marriottd6e3c902025-02-18 20:45:48 +01002437 plen = (size_t)(p - curbuf->b_ffname);
2438 if (plen + buflen >= MAXPATHL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002439 continue;
2440 if (buf[1] == NUL)
John Marriottd6e3c902025-02-18 20:45:48 +01002441 buf[plen] = NUL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002442 else
John Marriottd6e3c902025-02-18 20:45:48 +01002443 mch_memmove(buf + plen, buf + 2, (buflen - 2) + 1); // +1 for NUL
2444 mch_memmove(buf, curbuf->b_ffname, plen);
2445 buflen = simplify_filename(buf);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002446 }
2447 else if (buf[0] == NUL)
John Marriottd6e3c902025-02-18 20:45:48 +01002448 {
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002449 // relative to current directory
2450 STRCPY(buf, curdir);
John Marriottd6e3c902025-02-18 20:45:48 +01002451 if (curdirlen == 0)
2452 curdirlen = STRLEN(curdir);
2453 buflen = curdirlen;
2454 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002455 else if (path_with_url(buf))
2456 // URL can't be used here
2457 continue;
2458 else if (!mch_isFullName(buf))
2459 {
2460 // Expand relative path to their full path equivalent
John Marriottd6e3c902025-02-18 20:45:48 +01002461 if (curdirlen == 0)
2462 curdirlen = STRLEN(curdir);
2463 if (curdirlen + buflen + 3 > MAXPATHL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002464 continue;
John Marriottd6e3c902025-02-18 20:45:48 +01002465
2466 mch_memmove(buf + curdirlen + 1, buf, buflen + 1); // +1 for NUL
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002467 STRCPY(buf, curdir);
John Marriottd6e3c902025-02-18 20:45:48 +01002468 buf[curdirlen] = PATHSEP;
2469 buflen = simplify_filename(buf);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002470 }
2471
2472 if (ga_grow(gap, 1) == FAIL)
2473 break;
2474
2475# if defined(MSWIN)
2476 // Avoid the path ending in a backslash, it fails when a comma is
2477 // appended.
John Marriottd6e3c902025-02-18 20:45:48 +01002478 if (buf[buflen - 1] == '\\')
2479 buf[buflen - 1] = '/';
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002480# endif
2481
John Marriottd6e3c902025-02-18 20:45:48 +01002482 p = vim_strnsave(buf, buflen);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002483 if (p == NULL)
2484 break;
2485 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
2486 }
2487
2488 vim_free(buf);
2489}
2490
2491/*
2492 * Returns a pointer to the file or directory name in "fname" that matches the
2493 * longest path in "ga"p, or NULL if there is no match. For example:
2494 *
2495 * path: /foo/bar/baz
2496 * fname: /foo/bar/baz/quux.txt
2497 * returns: ^this
2498 */
2499 static char_u *
2500get_path_cutoff(char_u *fname, garray_T *gap)
2501{
2502 int i;
2503 int maxlen = 0;
2504 char_u **path_part = (char_u **)gap->ga_data;
2505 char_u *cutoff = NULL;
2506
2507 for (i = 0; i < gap->ga_len; i++)
2508 {
2509 int j = 0;
2510
2511 while ((fname[j] == path_part[i][j]
2512# if defined(MSWIN)
2513 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
2514# endif
2515 ) && fname[j] != NUL && path_part[i][j] != NUL)
2516 j++;
2517 if (j > maxlen)
2518 {
2519 maxlen = j;
2520 cutoff = &fname[j];
2521 }
2522 }
2523
2524 // skip to the file or directory name
2525 if (cutoff != NULL)
2526 while (vim_ispathsep(*cutoff))
2527 MB_PTR_ADV(cutoff);
2528
2529 return cutoff;
2530}
2531
2532/*
2533 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
2534 * that they are unique with respect to each other while conserving the part
2535 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
2536 */
2537 void
LemonBoya20bf692024-07-11 22:35:53 +02002538uniquefy_paths(
2539 garray_T *gap,
2540 char_u *pattern,
2541 char_u *path_option) // p_path or p_cdpath
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002542{
2543 int i;
2544 int len;
2545 char_u **fnames = (char_u **)gap->ga_data;
2546 int sort_again = FALSE;
2547 char_u *pat;
2548 char_u *file_pattern;
2549 char_u *curdir;
2550 regmatch_T regmatch;
2551 garray_T path_ga;
2552 char_u **in_curdir = NULL;
2553 char_u *short_name;
2554
2555 remove_duplicates(gap);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002556 ga_init2(&path_ga, sizeof(char_u *), 1);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002557
2558 /*
2559 * We need to prepend a '*' at the beginning of file_pattern so that the
2560 * regex matches anywhere in the path. FIXME: is this valid for all
2561 * possible patterns?
2562 */
2563 len = (int)STRLEN(pattern);
2564 file_pattern = alloc(len + 2);
2565 if (file_pattern == NULL)
2566 return;
2567 file_pattern[0] = '*';
John Marriottd6e3c902025-02-18 20:45:48 +01002568 STRCPY(file_pattern + 1, pattern);
Christian Brabandt1a31c432024-10-06 16:34:20 +02002569 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, FALSE);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002570 vim_free(file_pattern);
2571 if (pat == NULL)
2572 return;
2573
2574 regmatch.rm_ic = TRUE; // always ignore case
2575 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2576 vim_free(pat);
2577 if (regmatch.regprog == NULL)
2578 return;
2579
Bram Moolenaar51e14382019-05-25 20:21:28 +02002580 if ((curdir = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002581 goto theend;
2582 mch_dirname(curdir, MAXPATHL);
LemonBoya20bf692024-07-11 22:35:53 +02002583 expand_path_option(curdir, path_option, &path_ga);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002584
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002585 in_curdir = ALLOC_CLEAR_MULT(char_u *, gap->ga_len);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002586 if (in_curdir == NULL)
2587 goto theend;
2588
2589 for (i = 0; i < gap->ga_len && !got_int; i++)
2590 {
2591 char_u *path = fnames[i];
2592 int is_in_curdir;
2593 char_u *dir_end = gettail_dir(path);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002594 char_u *path_cutoff;
2595
2596 len = (int)STRLEN(path);
2597 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
2598 && curdir[dir_end - path] == NUL;
2599 if (is_in_curdir)
John Marriottd6e3c902025-02-18 20:45:48 +01002600 in_curdir[i] = vim_strnsave(path, len);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002601
2602 // Shorten the filename while maintaining its uniqueness
2603 path_cutoff = get_path_cutoff(path, &path_ga);
2604
2605 // Don't assume all files can be reached without path when search
2606 // pattern starts with star star slash, so only remove path_cutoff
2607 // when possible.
2608 if (pattern[0] == '*' && pattern[1] == '*'
2609 && vim_ispathsep_nocolon(pattern[2])
2610 && path_cutoff != NULL
2611 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
2612 && is_unique(path_cutoff, gap, i))
2613 {
2614 sort_again = TRUE;
2615 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
2616 }
2617 else
2618 {
2619 // Here all files can be reached without path, so get shortest
2620 // unique path. We start at the end of the path.
John Marriottd6e3c902025-02-18 20:45:48 +01002621 char_u *pathsep_p = path + len - 1;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002622
2623 while (find_previous_pathsep(path, &pathsep_p))
John Marriottd6e3c902025-02-18 20:45:48 +01002624 {
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002625 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
2626 && is_unique(pathsep_p + 1, gap, i)
2627 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
2628 {
2629 sort_again = TRUE;
John Marriottd6e3c902025-02-18 20:45:48 +01002630 mch_memmove(path, pathsep_p + 1,
2631 (size_t)((path + len) - (pathsep_p + 1)) + 1); // +1 for NUL
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002632 break;
2633 }
John Marriottd6e3c902025-02-18 20:45:48 +01002634 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002635 }
2636
2637 if (mch_isFullName(path))
2638 {
2639 /*
2640 * Last resort: shorten relative to curdir if possible.
2641 * 'possible' means:
2642 * 1. It is under the current directory.
2643 * 2. The result is actually shorter than the original.
2644 *
2645 * Before curdir After
2646 * /foo/bar/file.txt /foo/bar ./file.txt
2647 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
2648 * /file.txt / /file.txt
2649 * c:\file.txt c:\ .\file.txt
2650 */
2651 short_name = shorten_fname(path, curdir);
2652 if (short_name != NULL && short_name > path + 1
2653# if defined(MSWIN)
2654 // On windows,
2655 // shorten_fname("c:\a\a.txt", "c:\a\b")
2656 // returns "\a\a.txt", which is not really the short
2657 // name, hence:
2658 && !vim_ispathsep(*short_name)
2659# endif
2660 )
2661 {
John Marriottd6e3c902025-02-18 20:45:48 +01002662 vim_snprintf((char *)path, MAXPATHL, ".%s%s", PATHSEPSTR, short_name);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002663 }
2664 }
2665 ui_breakcheck();
2666 }
2667
2668 // Shorten filenames in /in/current/directory/{filename}
2669 for (i = 0; i < gap->ga_len && !got_int; i++)
2670 {
John Marriottd6e3c902025-02-18 20:45:48 +01002671 size_t rel_pathsize;
2672 char_u *rel_path;
2673 char_u *path = in_curdir[i];
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002674
2675 if (path == NULL)
2676 continue;
2677
2678 // If the {filename} is not unique, change it to ./{filename}.
2679 // Else reduce it to {filename}
2680 short_name = shorten_fname(path, curdir);
2681 if (short_name == NULL)
2682 short_name = path;
2683 if (is_unique(short_name, gap, i))
2684 {
2685 STRCPY(fnames[i], short_name);
2686 continue;
2687 }
2688
John Marriottd6e3c902025-02-18 20:45:48 +01002689 rel_pathsize = 1 + STRLEN_LITERAL(PATHSEPSTR) + STRLEN(short_name) + 1;
2690 rel_path = alloc(rel_pathsize);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002691 if (rel_path == NULL)
2692 goto theend;
John Marriottd6e3c902025-02-18 20:45:48 +01002693
2694 vim_snprintf((char *)rel_path, rel_pathsize, ".%s%s", PATHSEPSTR, short_name);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002695
2696 vim_free(fnames[i]);
2697 fnames[i] = rel_path;
2698 sort_again = TRUE;
2699 ui_breakcheck();
2700 }
2701
2702theend:
2703 vim_free(curdir);
2704 if (in_curdir != NULL)
2705 {
2706 for (i = 0; i < gap->ga_len; i++)
2707 vim_free(in_curdir[i]);
2708 vim_free(in_curdir);
2709 }
2710 ga_clear_strings(&path_ga);
2711 vim_regfree(regmatch.regprog);
2712
2713 if (sort_again)
2714 remove_duplicates(gap);
2715}
2716
2717/*
2718 * Calls globpath() with 'path' values for the given pattern and stores the
2719 * result in "gap".
2720 * Returns the total number of matches.
2721 */
2722 int
2723expand_in_path(
2724 garray_T *gap,
2725 char_u *pattern,
2726 int flags) // EW_* flags
2727{
2728 char_u *curdir;
2729 garray_T path_ga;
2730 char_u *paths = NULL;
2731 int glob_flags = 0;
LemonBoya20bf692024-07-11 22:35:53 +02002732 char_u *path_option = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002733
Bram Moolenaar964b3742019-05-24 18:54:09 +02002734 if ((curdir = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002735 return 0;
2736 mch_dirname(curdir, MAXPATHL);
2737
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002738 ga_init2(&path_ga, sizeof(char_u *), 1);
LemonBoya20bf692024-07-11 22:35:53 +02002739 if (flags & EW_CDPATH)
2740 expand_path_option(curdir, p_cdpath, &path_ga);
2741 else
2742 expand_path_option(curdir, path_option, &path_ga);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002743 vim_free(curdir);
2744 if (path_ga.ga_len == 0)
2745 return 0;
2746
2747 paths = ga_concat_strings(&path_ga, ",");
2748 ga_clear_strings(&path_ga);
2749 if (paths == NULL)
2750 return 0;
2751
2752 if (flags & EW_ICASE)
2753 glob_flags |= WILD_ICASE;
2754 if (flags & EW_ADDSLASH)
2755 glob_flags |= WILD_ADD_SLASH;
LemonBoya20bf692024-07-11 22:35:53 +02002756 globpath(paths, pattern, gap, glob_flags, !!(flags & EW_CDPATH));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002757 vim_free(paths);
2758
2759 return gap->ga_len;
2760}
2761
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002762
2763/*
2764 * Converts a file name into a canonical form. It simplifies a file name into
2765 * its simplest form by stripping out unneeded components, if any. The
2766 * resulting file name is simplified in place and will either be the same
2767 * length as that supplied, or shorter.
2768 */
John Marriottd6e3c902025-02-18 20:45:48 +01002769 size_t
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002770simplify_filename(char_u *filename)
2771{
2772#ifndef AMIGA // Amiga doesn't have "..", it uses "/"
2773 int components = 0;
2774 char_u *p, *tail, *start;
John Marriottd6e3c902025-02-18 20:45:48 +01002775 char_u *p_end; // point to NUL at end of string "p"
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002776 int stripping_disabled = FALSE;
2777 int relative = TRUE;
2778
2779 p = filename;
2780# ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmanan6df0f272021-12-16 13:06:10 +00002781 if (p[0] != NUL && p[1] == ':') // skip "x:"
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002782 p += 2;
2783# endif
2784
2785 if (vim_ispathsep(*p))
2786 {
2787 relative = FALSE;
2788 do
2789 ++p;
2790 while (vim_ispathsep(*p));
2791 }
2792 start = p; // remember start after "c:/" or "/" or "///"
John Marriottd6e3c902025-02-18 20:45:48 +01002793 p_end = p + STRLEN(p);
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02002794#ifdef UNIX
2795 // Posix says that "//path" is unchanged but "///path" is "/path".
2796 if (start > filename + 2)
2797 {
John Marriottd6e3c902025-02-18 20:45:48 +01002798 mch_memmove(filename + 1, p, (size_t)(p_end - p) + 1); // +1 for NUL
2799 p_end -= (size_t)(p - (filename + 1));
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02002800 start = p = filename + 1;
2801 }
2802#endif
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002803
2804 do
2805 {
2806 // At this point "p" is pointing to the char following a single "/"
2807 // or "p" is at the "start" of the (absolute or relative) path name.
2808# ifdef VMS
2809 // VMS allows device:[path] - don't strip the [ in directory
2810 if ((*p == '[' || *p == '<') && p > filename && p[-1] == ':')
2811 {
2812 // :[ or :< composition: vms directory component
2813 ++components;
2814 p = getnextcomp(p + 1);
2815 }
2816 // allow remote calls as host"user passwd"::device:[path]
2817 else if (p[0] == ':' && p[1] == ':' && p > filename && p[-1] == '"' )
2818 {
2819 // ":: composition: vms host/passwd component
2820 ++components;
2821 p = getnextcomp(p + 2);
2822 }
2823 else
2824# endif
John Marriottd6e3c902025-02-18 20:45:48 +01002825 if (vim_ispathsep(*p))
2826 {
2827 mch_memmove(p, p + 1, (size_t)(p_end - (p + 1)) + 1); // remove duplicate "/"
2828 --p_end;
2829 }
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002830 else if (p[0] == '.' && (vim_ispathsep(p[1]) || p[1] == NUL))
2831 {
2832 if (p == start && relative)
2833 p += 1 + (p[1] != NUL); // keep single "." or leading "./"
2834 else
2835 {
2836 // Strip "./" or ".///". If we are at the end of the file name
2837 // and there is no trailing path separator, either strip "/." if
2838 // we are after "start", or strip "." if we are at the beginning
2839 // of an absolute path name .
2840 tail = p + 1;
2841 if (p[1] != NUL)
2842 while (vim_ispathsep(*tail))
2843 MB_PTR_ADV(tail);
2844 else if (p > start)
2845 --p; // strip preceding path separator
John Marriottd6e3c902025-02-18 20:45:48 +01002846
2847 mch_memmove(p, tail, (size_t)(p_end - tail) + 1);
2848 p_end -= (size_t)(tail - p);
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002849 }
2850 }
2851 else if (p[0] == '.' && p[1] == '.' &&
2852 (vim_ispathsep(p[2]) || p[2] == NUL))
2853 {
2854 // Skip to after ".." or "../" or "..///".
2855 tail = p + 2;
2856 while (vim_ispathsep(*tail))
2857 MB_PTR_ADV(tail);
2858
2859 if (components > 0) // strip one preceding component
2860 {
2861 int do_strip = FALSE;
2862 char_u saved_char;
2863 stat_T st;
2864
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002865 // Don't strip for an erroneous file name.
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002866 if (!stripping_disabled)
2867 {
2868 // If the preceding component does not exist in the file
2869 // system, we strip it. On Unix, we don't accept a symbolic
2870 // link that refers to a non-existent file.
2871 saved_char = p[-1];
2872 p[-1] = NUL;
2873# ifdef UNIX
2874 if (mch_lstat((char *)filename, &st) < 0)
2875# else
2876 if (mch_stat((char *)filename, &st) < 0)
2877# endif
2878 do_strip = TRUE;
2879 p[-1] = saved_char;
2880
2881 --p;
2882 // Skip back to after previous '/'.
2883 while (p > start && !after_pathsep(start, p))
2884 MB_PTR_BACK(start, p);
2885
2886 if (!do_strip)
2887 {
2888 // If the component exists in the file system, check
2889 // that stripping it won't change the meaning of the
2890 // file name. First get information about the
2891 // unstripped file name. This may fail if the component
2892 // to strip is not a searchable directory (but a regular
2893 // file, for instance), since the trailing "/.." cannot
2894 // be applied then. We don't strip it then since we
2895 // don't want to replace an erroneous file name by
2896 // a valid one, and we disable stripping of later
2897 // components.
2898 saved_char = *tail;
2899 *tail = NUL;
2900 if (mch_stat((char *)filename, &st) >= 0)
2901 do_strip = TRUE;
2902 else
2903 stripping_disabled = TRUE;
2904 *tail = saved_char;
2905# ifdef UNIX
2906 if (do_strip)
2907 {
2908 stat_T new_st;
2909
2910 // On Unix, the check for the unstripped file name
2911 // above works also for a symbolic link pointing to
2912 // a searchable directory. But then the parent of
2913 // the directory pointed to by the link must be the
2914 // same as the stripped file name. (The latter
2915 // exists in the file system since it is the
2916 // component's parent directory.)
2917 if (p == start && relative)
2918 (void)mch_stat(".", &new_st);
2919 else
2920 {
2921 saved_char = *p;
2922 *p = NUL;
2923 (void)mch_stat((char *)filename, &new_st);
2924 *p = saved_char;
2925 }
2926
2927 if (new_st.st_ino != st.st_ino ||
2928 new_st.st_dev != st.st_dev)
2929 {
2930 do_strip = FALSE;
2931 // We don't disable stripping of later
2932 // components since the unstripped path name is
2933 // still valid.
2934 }
2935 }
2936# endif
2937 }
2938 }
2939
2940 if (!do_strip)
2941 {
2942 // Skip the ".." or "../" and reset the counter for the
2943 // components that might be stripped later on.
2944 p = tail;
2945 components = 0;
2946 }
2947 else
2948 {
2949 // Strip previous component. If the result would get empty
2950 // and there is no trailing path separator, leave a single
2951 // "." instead. If we are at the end of the file name and
2952 // there is no trailing path separator and a preceding
2953 // component is left after stripping, strip its trailing
2954 // path separator as well.
2955 if (p == start && relative && tail[-1] == '.')
2956 {
2957 *p++ = '.';
2958 *p = NUL;
2959 }
2960 else
2961 {
2962 if (p > start && tail[-1] == '.')
2963 --p;
John Marriottd6e3c902025-02-18 20:45:48 +01002964
2965 mch_memmove(p, tail, (size_t)(p_end - tail) + 1); // strip previous component
2966 p_end -= (size_t)(tail - p);
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002967 }
2968
2969 --components;
2970 }
2971 }
2972 else if (p == start && !relative) // leading "/.." or "/../"
John Marriottd6e3c902025-02-18 20:45:48 +01002973 {
2974 mch_memmove(p, tail, (size_t)(p_end - tail) + 1); // strip ".." or "../"
2975 p_end -= (size_t)(tail - p);
2976 }
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002977 else
2978 {
2979 if (p == start + 2 && p[-2] == '.') // leading "./../"
2980 {
John Marriottd6e3c902025-02-18 20:45:48 +01002981 mch_memmove(p - 2, p, (size_t)(p_end - p) + 1); // strip leading "./"
2982 p_end -= 2;
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002983 tail -= 2;
2984 }
2985 p = tail; // skip to char after ".." or "../"
2986 }
2987 }
2988 else
2989 {
2990 ++components; // simple path component
2991 p = getnextcomp(p);
2992 }
2993 } while (*p != NUL);
2994#endif // !AMIGA
John Marriottd6e3c902025-02-18 20:45:48 +01002995
2996 return (size_t)(p_end - filename);
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002997}
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002998
2999#if defined(FEAT_EVAL) || defined(PROTO)
3000/*
3001 * "simplify()" function
3002 */
3003 void
3004f_simplify(typval_T *argvars, typval_T *rettv)
3005{
3006 char_u *p;
3007
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02003008 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
3009 return;
3010
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02003011 p = tv_get_string_strict(&argvars[0]);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02003012 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003013 simplify_filename(rettv->vval.v_string); // simplify in place
Bram Moolenaarb005cd82019-09-04 15:54:55 +02003014 rettv->v_type = VAR_STRING;
3015}
3016#endif // FEAT_EVAL