blob: 149f7c685e518d84b40a7db8e232de2fad2f76c5 [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
69 char_u *ffs_fix_path;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010070 char_u *ffs_wc_path;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010071
72 // files/dirs found in the above directory, matched by the first wildcard
73 // of wc_part
74 char_u **ffs_filearray;
75 int ffs_filearray_size;
John Drouhard95fca122022-08-01 11:38:17 +010076 int ffs_filearray_cur; // needed for partly handled dirs
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010077
78 // to store status of partly handled directories
79 // 0: we work on this directory for the first time
80 // 1: this directory was partly searched in an earlier step
81 int ffs_stage;
82
83 // How deep are we in the directory tree?
84 // Counts backward from value of level parameter to vim_findfile_init
85 int ffs_level;
86
87 // Did we already expand '**' to an empty string?
88 int ffs_star_star_empty;
89} ff_stack_T;
90
91/*
92 * type for already visited directories or files.
93 */
94typedef struct ff_visited
95{
96 struct ff_visited *ffv_next;
97
Bram Moolenaar5fd0f502019-02-13 23:13:28 +010098 // Visited directories are different if the wildcard string are
99 // different. So we have to save it.
100 char_u *ffv_wc_path;
Bram Moolenaar2bd9dbc2022-08-25 18:12:06 +0100101
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100102 // for unix use inode etc for comparison (needed because of links), else
103 // use filename.
104#ifdef UNIX
105 int ffv_dev_valid; // ffv_dev and ffv_ino were set
106 dev_t ffv_dev; // device number
107 ino_t ffv_ino; // inode number
108#endif
109 // The memory for this struct is allocated according to the length of
110 // ffv_fname.
111 char_u ffv_fname[1]; // actually longer
112} ff_visited_T;
113
114/*
115 * We might have to manage several visited lists during a search.
116 * This is especially needed for the tags option. If tags is set to:
117 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
118 * So we have to do 3 searches:
119 * 1) search from the current files directory downward for the file "tags"
120 * 2) search from the current files directory downward for the file "TAGS"
121 * 3) search from Vims current directory downwards for the file "tags"
122 * As you can see, the first and the third search are for the same file, so for
123 * the third search we can use the visited list of the first search. For the
124 * second search we must start from a empty visited list.
125 * The struct ff_visited_list_hdr is used to manage a linked list of already
126 * visited lists.
127 */
128typedef struct ff_visited_list_hdr
129{
130 struct ff_visited_list_hdr *ffvl_next;
131
132 // the filename the attached visited list is for
133 char_u *ffvl_filename;
134
135 ff_visited_T *ffvl_visited_list;
136
137} ff_visited_list_hdr_T;
138
139
140/*
141 * '**' can be expanded to several directory levels.
142 * Set the default maximum depth.
143 */
144#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
145
146/*
147 * The search context:
148 * ffsc_stack_ptr: the stack for the dirs to search
149 * ffsc_visited_list: the currently active visited list
150 * ffsc_dir_visited_list: the currently active visited list for search dirs
151 * ffsc_visited_lists_list: the list of all visited lists
152 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
153 * ffsc_file_to_search: the file to search for
154 * ffsc_start_dir: the starting directory, if search path was relative
155 * ffsc_fix_path: the fix part of the given path (without wildcards)
156 * Needed for upward search.
157 * ffsc_wc_path: the part of the given path containing wildcards
158 * ffsc_level: how many levels of dirs to search downwards
159 * ffsc_stopdirs_v: array of stop directories for upward search
160 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
161 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
162 */
163typedef struct ff_search_ctx_T
164{
165 ff_stack_T *ffsc_stack_ptr;
166 ff_visited_list_hdr_T *ffsc_visited_list;
167 ff_visited_list_hdr_T *ffsc_dir_visited_list;
168 ff_visited_list_hdr_T *ffsc_visited_lists_list;
169 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
170 char_u *ffsc_file_to_search;
171 char_u *ffsc_start_dir;
172 char_u *ffsc_fix_path;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100173 char_u *ffsc_wc_path;
174 int ffsc_level;
175 char_u **ffsc_stopdirs_v;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100176 int ffsc_find_what;
177 int ffsc_tagfile;
178} ff_search_ctx_T;
179
180// locally needed functions
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100181static int ff_check_visited(ff_visited_T **, char_u *, char_u *);
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);
185static ff_visited_list_hdr_T* ff_get_visited_list(char_u *, ff_visited_list_hdr_T **list_headp);
186
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);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100191static ff_stack_T *ff_create_stack_element(char_u *, char_u *, int, int);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100192static int ff_path_in_stoplist(char_u *, int, char_u **);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100193
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100194static char_u *ff_expand_buffer = NULL; // used for expanding filenames
195
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,
286 char_u *stopdirs UNUSED,
287 int level,
288 int free_visited,
289 int find_what,
290 void *search_ctx_arg,
291 int tagfile, // expanding names of tags files
292 char_u *rel_fname) // file name to use for "."
293{
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100294 char_u *wc_part;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100295 ff_stack_T *sptr;
296 ff_search_ctx_T *search_ctx;
297
298 // If a search context is given by the caller, reuse it, else allocate a
299 // new one.
300 if (search_ctx_arg != NULL)
301 search_ctx = search_ctx_arg;
302 else
303 {
Bram Moolenaara80faa82020-04-12 19:37:17 +0200304 search_ctx = ALLOC_CLEAR_ONE(ff_search_ctx_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100305 if (search_ctx == NULL)
306 goto error_return;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100307 }
308 search_ctx->ffsc_find_what = find_what;
309 search_ctx->ffsc_tagfile = tagfile;
310
311 // clear the search context, but NOT the visited lists
312 ff_clear(search_ctx);
313
314 // clear visited list if wanted
315 if (free_visited == TRUE)
316 vim_findfile_free_visited(search_ctx);
317 else
318 {
319 // Reuse old visited lists. Get the visited list for the given
320 // filename. If no list for the current filename exists, creates a new
321 // one.
322 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
323 &search_ctx->ffsc_visited_lists_list);
324 if (search_ctx->ffsc_visited_list == NULL)
325 goto error_return;
326 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
327 &search_ctx->ffsc_dir_visited_lists_list);
328 if (search_ctx->ffsc_dir_visited_list == NULL)
329 goto error_return;
330 }
331
332 if (ff_expand_buffer == NULL)
333 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200334 ff_expand_buffer = alloc(MAXPATHL);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100335 if (ff_expand_buffer == NULL)
336 goto error_return;
337 }
338
339 // Store information on starting dir now if path is relative.
340 // If path is absolute, we do that later.
341 if (path[0] == '.'
342 && (vim_ispathsep(path[1]) || path[1] == NUL)
343 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
344 && rel_fname != NULL)
345 {
346 int len = (int)(gettail(rel_fname) - rel_fname);
347
348 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
349 {
350 // Make the start dir an absolute path name.
351 vim_strncpy(ff_expand_buffer, rel_fname, len);
352 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
353 }
354 else
355 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
356 if (search_ctx->ffsc_start_dir == NULL)
357 goto error_return;
358 if (*++path != NUL)
359 ++path;
360 }
361 else if (*path == NUL || !vim_isAbsName(path))
362 {
363#ifdef BACKSLASH_IN_FILENAME
364 // "c:dir" needs "c:" to be expanded, otherwise use current dir
365 if (*path != NUL && path[1] == ':')
366 {
367 char_u drive[3];
368
369 drive[0] = path[0];
370 drive[1] = ':';
371 drive[2] = NUL;
372 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
373 goto error_return;
374 path += 2;
375 }
376 else
377#endif
378 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
379 goto error_return;
380
381 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
382 if (search_ctx->ffsc_start_dir == NULL)
383 goto error_return;
384
385#ifdef BACKSLASH_IN_FILENAME
386 // A path that starts with "/dir" is relative to the drive, not to the
387 // directory (but not for "//machine/dir"). Only use the drive name.
388 if ((*path == '/' || *path == '\\')
389 && path[1] != path[0]
390 && search_ctx->ffsc_start_dir[1] == ':')
391 search_ctx->ffsc_start_dir[2] = NUL;
392#endif
393 }
394
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100395 /*
396 * If stopdirs are given, split them into an array of pointers.
397 * If this fails (mem allocation), there is no upward search at all or a
398 * stop directory is not recognized -> continue silently.
399 * If stopdirs just contains a ";" or is empty,
400 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
401 * is handled as unlimited upward search. See function
402 * ff_path_in_stoplist() for details.
403 */
404 if (stopdirs != NULL)
405 {
406 char_u *walker = stopdirs;
407 int dircount;
408
409 while (*walker == ';')
410 walker++;
411
412 dircount = 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200413 search_ctx->ffsc_stopdirs_v = ALLOC_ONE(char_u *);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100414
415 if (search_ctx->ffsc_stopdirs_v != NULL)
416 {
417 do
418 {
419 char_u *helper;
420 void *ptr;
421
422 helper = walker;
423 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
424 (dircount + 1) * sizeof(char_u *));
425 if (ptr)
426 search_ctx->ffsc_stopdirs_v = ptr;
427 else
428 // ignore, keep what we have and continue
429 break;
430 walker = vim_strchr(walker, ';');
431 if (walker)
432 {
433 search_ctx->ffsc_stopdirs_v[dircount-1] =
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200434 vim_strnsave(helper, walker - helper);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100435 walker++;
436 }
437 else
438 // this might be "", which means ascent till top
439 // of directory tree.
440 search_ctx->ffsc_stopdirs_v[dircount-1] =
441 vim_strsave(helper);
442
443 dircount++;
444
445 } while (walker != NULL);
446 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
447 }
448 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100449
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100450 search_ctx->ffsc_level = level;
451
452 /*
453 * split into:
454 * -fix path
455 * -wildcard_stuff (might be NULL)
456 */
457 wc_part = vim_strchr(path, '*');
458 if (wc_part != NULL)
459 {
460 int llevel;
461 int len;
462 char *errpt;
463
464 // save the fix part of the path
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200465 search_ctx->ffsc_fix_path = vim_strnsave(path, wc_part - path);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100466
467 /*
468 * copy wc_path and add restricts to the '**' wildcard.
469 * The octet after a '**' is used as a (binary) counter.
470 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
471 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100472 * If no restrict is given after '**' the default is used.
473 * Due to this technique the path looks awful if you print it as a
474 * string.
475 */
476 len = 0;
477 while (*wc_part != NUL)
478 {
479 if (len + 5 >= MAXPATHL)
480 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000481 emsg(_(e_path_too_long_for_completion));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100482 break;
483 }
484 if (STRNCMP(wc_part, "**", 2) == 0)
485 {
486 ff_expand_buffer[len++] = *wc_part++;
487 ff_expand_buffer[len++] = *wc_part++;
488
489 llevel = strtol((char *)wc_part, &errpt, 10);
490 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
491 ff_expand_buffer[len++] = llevel;
492 else if ((char_u *)errpt != wc_part && llevel == 0)
493 // restrict is 0 -> remove already added '**'
494 len -= 2;
495 else
496 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
497 wc_part = (char_u *)errpt;
498 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
499 {
Bram Moolenaareaaac012022-01-02 17:00:40 +0000500 semsg(_(e_invalid_path_number_must_be_at_end_of_path_or_be_followed_by_str), PATHSEPSTR);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100501 goto error_return;
502 }
503 }
504 else
505 ff_expand_buffer[len++] = *wc_part++;
506 }
507 ff_expand_buffer[len] = NUL;
508 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
509
510 if (search_ctx->ffsc_wc_path == NULL)
511 goto error_return;
512 }
513 else
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100514 search_ctx->ffsc_fix_path = vim_strsave(path);
515
516 if (search_ctx->ffsc_start_dir == NULL)
517 {
518 // store the fix part as startdir.
519 // This is needed if the parameter path is fully qualified.
520 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
521 if (search_ctx->ffsc_start_dir == NULL)
522 goto error_return;
523 search_ctx->ffsc_fix_path[0] = NUL;
524 }
525
526 // create an absolute path
527 if (STRLEN(search_ctx->ffsc_start_dir)
528 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
529 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000530 emsg(_(e_path_too_long_for_completion));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100531 goto error_return;
532 }
533 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
534 add_pathsep(ff_expand_buffer);
535 {
536 int eb_len = (int)STRLEN(ff_expand_buffer);
537 char_u *buf = alloc(eb_len
538 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
539
540 STRCPY(buf, ff_expand_buffer);
541 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
542 if (mch_isdir(buf))
543 {
544 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
545 add_pathsep(ff_expand_buffer);
546 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100547 else
548 {
549 char_u *p = gettail(search_ctx->ffsc_fix_path);
550 char_u *wc_path = NULL;
551 char_u *temp = NULL;
552 int len = 0;
553
554 if (p > search_ctx->ffsc_fix_path)
555 {
Christian Brabandt7a4ca322021-07-25 15:08:05 +0200556 // do not add '..' to the path and start upwards searching
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100557 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
Christian Brabandt7a4ca322021-07-25 15:08:05 +0200558 if ((len >= 2
559 && STRNCMP(search_ctx->ffsc_fix_path, "..", 2) == 0)
560 && (len == 2
561 || search_ctx->ffsc_fix_path[2] == PATHSEP))
562 {
563 vim_free(buf);
564 goto error_return;
565 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100566 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
567 add_pathsep(ff_expand_buffer);
568 }
569 else
570 len = (int)STRLEN(search_ctx->ffsc_fix_path);
571
572 if (search_ctx->ffsc_wc_path != NULL)
573 {
574 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar51e14382019-05-25 20:21:28 +0200575 temp = alloc(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100576 + STRLEN(search_ctx->ffsc_fix_path + len)
Bram Moolenaar51e14382019-05-25 20:21:28 +0200577 + 1);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100578 if (temp == NULL || wc_path == NULL)
579 {
580 vim_free(buf);
581 vim_free(temp);
582 vim_free(wc_path);
583 goto error_return;
584 }
585
586 STRCPY(temp, search_ctx->ffsc_fix_path + len);
587 STRCAT(temp, search_ctx->ffsc_wc_path);
588 vim_free(search_ctx->ffsc_wc_path);
589 vim_free(wc_path);
590 search_ctx->ffsc_wc_path = temp;
591 }
592 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100593 vim_free(buf);
594 }
595
596 sptr = ff_create_stack_element(ff_expand_buffer,
Bram Moolenaar2bd9dbc2022-08-25 18:12:06 +0100597 search_ctx->ffsc_wc_path, level, 0);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100598
599 if (sptr == NULL)
600 goto error_return;
601
602 ff_push(search_ctx, sptr);
603
604 search_ctx->ffsc_file_to_search = vim_strsave(filename);
605 if (search_ctx->ffsc_file_to_search == NULL)
606 goto error_return;
607
608 return search_ctx;
609
610error_return:
611 /*
612 * We clear the search context now!
613 * Even when the caller gave us a (perhaps valid) context we free it here,
614 * as we might have already destroyed it.
615 */
616 vim_findfile_cleanup(search_ctx);
617 return NULL;
618}
619
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100620/*
621 * Get the stopdir string. Check that ';' is not escaped.
622 */
623 char_u *
624vim_findfile_stopdir(char_u *buf)
625{
626 char_u *r_ptr = buf;
627
628 while (*r_ptr != NUL && *r_ptr != ';')
629 {
630 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
631 {
632 // Overwrite the escape char,
633 // use STRLEN(r_ptr) to move the trailing '\0'.
634 STRMOVE(r_ptr, r_ptr + 1);
635 r_ptr++;
636 }
637 r_ptr++;
638 }
639 if (*r_ptr == ';')
640 {
641 *r_ptr = 0;
642 r_ptr++;
643 }
644 else if (*r_ptr == NUL)
645 r_ptr = NULL;
646 return r_ptr;
647}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100648
649/*
650 * Clean up the given search context. Can handle a NULL pointer.
651 */
652 void
653vim_findfile_cleanup(void *ctx)
654{
655 if (ctx == NULL)
656 return;
657
658 vim_findfile_free_visited(ctx);
659 ff_clear(ctx);
660 vim_free(ctx);
661}
662
663/*
664 * Find a file in a search context.
665 * The search context was created with vim_findfile_init() above.
666 * Return a pointer to an allocated file name or NULL if nothing found.
667 * To get all matching files call this function until you get NULL.
668 *
669 * If the passed search_context is NULL, NULL is returned.
670 *
671 * The search algorithm is depth first. To change this replace the
672 * stack with a list (don't forget to leave partly searched directories on the
673 * top of the list).
674 */
675 char_u *
676vim_findfile(void *search_ctx_arg)
677{
678 char_u *file_path;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100679 char_u *rest_of_wildcards;
680 char_u *path_end = NULL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100681 ff_stack_T *stackp;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100682 int len;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100683 int i;
684 char_u *p;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100685 char_u *suf;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100686 ff_search_ctx_T *search_ctx;
687
688 if (search_ctx_arg == NULL)
689 return NULL;
690
691 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
692
693 /*
694 * filepath is used as buffer for various actions and as the storage to
695 * return a found filename.
696 */
Bram Moolenaar51e14382019-05-25 20:21:28 +0200697 if ((file_path = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100698 return NULL;
699
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100700 // store the end of the start dir -- needed for upward search
701 if (search_ctx->ffsc_start_dir != NULL)
702 path_end = &search_ctx->ffsc_start_dir[
703 STRLEN(search_ctx->ffsc_start_dir)];
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100704
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100705 // upward search loop
706 for (;;)
707 {
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100708 // downward search loop
709 for (;;)
710 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000711 // check if user wants to stop the search
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100712 ui_breakcheck();
713 if (got_int)
714 break;
715
716 // get directory to work on from stack
717 stackp = ff_pop(search_ctx);
718 if (stackp == NULL)
719 break;
720
721 /*
722 * TODO: decide if we leave this test in
723 *
724 * GOOD: don't search a directory(-tree) twice.
725 * BAD: - check linked list for every new directory entered.
726 * - check for double files also done below
727 *
728 * Here we check if we already searched this directory.
729 * We already searched a directory if:
730 * 1) The directory is the same.
731 * 2) We would use the same wildcard string.
732 *
733 * Good if you have links on same directory via several ways
734 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
735 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
736 *
737 * This check is only needed for directories we work on for the
738 * first time (hence stackp->ff_filearray == NULL)
739 */
740 if (stackp->ffs_filearray == NULL
741 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
742 ->ffvl_visited_list,
Bram Moolenaar2bd9dbc2022-08-25 18:12:06 +0100743 stackp->ffs_fix_path, stackp->ffs_wc_path) == FAIL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100744 {
745#ifdef FF_VERBOSE
746 if (p_verbose >= 5)
747 {
748 verbose_enter_scroll();
749 smsg("Already Searched: %s (%s)",
750 stackp->ffs_fix_path, stackp->ffs_wc_path);
751 // don't overwrite this either
752 msg_puts("\n");
753 verbose_leave_scroll();
754 }
755#endif
756 ff_free_stack_element(stackp);
757 continue;
758 }
759#ifdef FF_VERBOSE
760 else if (p_verbose >= 5)
761 {
762 verbose_enter_scroll();
763 smsg("Searching: %s (%s)",
764 stackp->ffs_fix_path, stackp->ffs_wc_path);
765 // don't overwrite this either
766 msg_puts("\n");
767 verbose_leave_scroll();
768 }
769#endif
770
771 // check depth
772 if (stackp->ffs_level <= 0)
773 {
774 ff_free_stack_element(stackp);
775 continue;
776 }
777
778 file_path[0] = NUL;
779
780 /*
781 * If no filearray till now expand wildcards
782 * The function expand_wildcards() can handle an array of paths
783 * and all possible expands are returned in one array. We use this
784 * to handle the expansion of '**' into an empty string.
785 */
786 if (stackp->ffs_filearray == NULL)
787 {
788 char_u *dirptrs[2];
789
790 // we use filepath to build the path expand_wildcards() should
791 // expand.
792 dirptrs[0] = file_path;
793 dirptrs[1] = NULL;
794
795 // if we have a start dir copy it in
796 if (!vim_isAbsName(stackp->ffs_fix_path)
797 && search_ctx->ffsc_start_dir)
798 {
799 if (STRLEN(search_ctx->ffsc_start_dir) + 1 < MAXPATHL)
800 {
801 STRCPY(file_path, search_ctx->ffsc_start_dir);
802 add_pathsep(file_path);
803 }
804 else
805 {
806 ff_free_stack_element(stackp);
807 goto fail;
808 }
809 }
810
811 // append the fix part of the search path
812 if (STRLEN(file_path) + STRLEN(stackp->ffs_fix_path) + 1
813 < MAXPATHL)
814 {
815 STRCAT(file_path, stackp->ffs_fix_path);
816 add_pathsep(file_path);
817 }
818 else
819 {
820 ff_free_stack_element(stackp);
821 goto fail;
822 }
823
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100824 rest_of_wildcards = stackp->ffs_wc_path;
825 if (*rest_of_wildcards != NUL)
826 {
827 len = (int)STRLEN(file_path);
828 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
829 {
830 // pointer to the restrict byte
831 // The restrict byte is not a character!
832 p = rest_of_wildcards + 2;
833
834 if (*p > 0)
835 {
836 (*p)--;
837 if (len + 1 < MAXPATHL)
838 file_path[len++] = '*';
839 else
840 {
841 ff_free_stack_element(stackp);
842 goto fail;
843 }
844 }
845
846 if (*p == 0)
847 {
848 // remove '**<numb> from wildcards
849 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
850 }
851 else
852 rest_of_wildcards += 3;
853
854 if (stackp->ffs_star_star_empty == 0)
855 {
856 // if not done before, expand '**' to empty
857 stackp->ffs_star_star_empty = 1;
858 dirptrs[1] = stackp->ffs_fix_path;
859 }
860 }
861
862 /*
863 * Here we copy until the next path separator or the end of
864 * the path. If we stop at a path separator, there is
865 * still something else left. This is handled below by
866 * pushing every directory returned from expand_wildcards()
867 * on the stack again for further search.
868 */
869 while (*rest_of_wildcards
870 && !vim_ispathsep(*rest_of_wildcards))
871 if (len + 1 < MAXPATHL)
872 file_path[len++] = *rest_of_wildcards++;
873 else
874 {
875 ff_free_stack_element(stackp);
876 goto fail;
877 }
878
879 file_path[len] = NUL;
880 if (vim_ispathsep(*rest_of_wildcards))
881 rest_of_wildcards++;
882 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100883
884 /*
885 * Expand wildcards like "*" and "$VAR".
886 * If the path is a URL don't try this.
887 */
888 if (path_with_url(dirptrs[0]))
889 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200890 stackp->ffs_filearray = ALLOC_ONE(char_u *);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100891 if (stackp->ffs_filearray != NULL
892 && (stackp->ffs_filearray[0]
893 = vim_strsave(dirptrs[0])) != NULL)
894 stackp->ffs_filearray_size = 1;
895 else
896 stackp->ffs_filearray_size = 0;
897 }
898 else
899 // Add EW_NOTWILD because the expanded path may contain
900 // wildcard characters that are to be taken literally.
901 // This is a bit of a hack.
902 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
903 &stackp->ffs_filearray_size,
904 &stackp->ffs_filearray,
905 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
906
907 stackp->ffs_filearray_cur = 0;
908 stackp->ffs_stage = 0;
909 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100910 else
911 rest_of_wildcards = &stackp->ffs_wc_path[
912 STRLEN(stackp->ffs_wc_path)];
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100913
914 if (stackp->ffs_stage == 0)
915 {
916 // this is the first time we work on this directory
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100917 if (*rest_of_wildcards == NUL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100918 {
919 /*
920 * We don't have further wildcards to expand, so we have to
921 * check for the final file now.
922 */
923 for (i = stackp->ffs_filearray_cur;
924 i < stackp->ffs_filearray_size; ++i)
925 {
926 if (!path_with_url(stackp->ffs_filearray[i])
927 && !mch_isdir(stackp->ffs_filearray[i]))
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100928 continue; // not a directory
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100929
930 // prepare the filename to be checked for existence
931 // below
932 if (STRLEN(stackp->ffs_filearray[i]) + 1
933 + STRLEN(search_ctx->ffsc_file_to_search)
934 < MAXPATHL)
935 {
936 STRCPY(file_path, stackp->ffs_filearray[i]);
937 add_pathsep(file_path);
938 STRCAT(file_path, search_ctx->ffsc_file_to_search);
939 }
940 else
941 {
942 ff_free_stack_element(stackp);
943 goto fail;
944 }
945
946 /*
947 * Try without extra suffix and then with suffixes
948 * from 'suffixesadd'.
949 */
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100950 len = (int)STRLEN(file_path);
951 if (search_ctx->ffsc_tagfile)
952 suf = (char_u *)"";
953 else
954 suf = curbuf->b_p_sua;
955 for (;;)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100956 {
957 // if file exists and we didn't already find it
958 if ((path_with_url(file_path)
959 || (mch_getperm(file_path) >= 0
960 && (search_ctx->ffsc_find_what
961 == FINDFILE_BOTH
962 || ((search_ctx->ffsc_find_what
963 == FINDFILE_DIR)
964 == mch_isdir(file_path)))))
965#ifndef FF_VERBOSE
966 && (ff_check_visited(
Bram Moolenaar2bd9dbc2022-08-25 18:12:06 +0100967 &search_ctx->ffsc_visited_list
968 ->ffvl_visited_list,
969 file_path, (char_u *)"") == OK)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100970#endif
971 )
972 {
973#ifdef FF_VERBOSE
974 if (ff_check_visited(
Bram Moolenaar2bd9dbc2022-08-25 18:12:06 +0100975 &search_ctx->ffsc_visited_list
976 ->ffvl_visited_list,
977 file_path, (char_u *)"") == FAIL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100978 {
979 if (p_verbose >= 5)
980 {
981 verbose_enter_scroll();
982 smsg("Already: %s",
983 file_path);
984 // don't overwrite this either
985 msg_puts("\n");
986 verbose_leave_scroll();
987 }
988 continue;
989 }
990#endif
991
992 // push dir to examine rest of subdirs later
993 stackp->ffs_filearray_cur = i + 1;
994 ff_push(search_ctx, stackp);
995
996 if (!path_with_url(file_path))
997 simplify_filename(file_path);
998 if (mch_dirname(ff_expand_buffer, MAXPATHL)
999 == OK)
1000 {
1001 p = shorten_fname(file_path,
1002 ff_expand_buffer);
1003 if (p != NULL)
1004 STRMOVE(file_path, p);
1005 }
1006#ifdef FF_VERBOSE
1007 if (p_verbose >= 5)
1008 {
1009 verbose_enter_scroll();
1010 smsg("HIT: %s", file_path);
1011 // don't overwrite this either
1012 msg_puts("\n");
1013 verbose_leave_scroll();
1014 }
1015#endif
1016 return file_path;
1017 }
1018
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001019 // Not found or found already, try next suffix.
1020 if (*suf == NUL)
1021 break;
1022 copy_option_part(&suf, file_path + len,
1023 MAXPATHL - len, ",");
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001024 }
1025 }
1026 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001027 else
1028 {
1029 /*
1030 * still wildcards left, push the directories for further
1031 * search
1032 */
1033 for (i = stackp->ffs_filearray_cur;
1034 i < stackp->ffs_filearray_size; ++i)
1035 {
1036 if (!mch_isdir(stackp->ffs_filearray[i]))
1037 continue; // not a directory
1038
1039 ff_push(search_ctx,
1040 ff_create_stack_element(
1041 stackp->ffs_filearray[i],
1042 rest_of_wildcards,
1043 stackp->ffs_level - 1, 0));
1044 }
1045 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001046 stackp->ffs_filearray_cur = 0;
1047 stackp->ffs_stage = 1;
1048 }
1049
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001050 /*
1051 * if wildcards contains '**' we have to descent till we reach the
1052 * leaves of the directory tree.
1053 */
1054 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
1055 {
1056 for (i = stackp->ffs_filearray_cur;
1057 i < stackp->ffs_filearray_size; ++i)
1058 {
1059 if (fnamecmp(stackp->ffs_filearray[i],
1060 stackp->ffs_fix_path) == 0)
1061 continue; // don't repush same directory
1062 if (!mch_isdir(stackp->ffs_filearray[i]))
1063 continue; // not a directory
1064 ff_push(search_ctx,
1065 ff_create_stack_element(stackp->ffs_filearray[i],
1066 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
1067 }
1068 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001069
1070 // we are done with the current directory
1071 ff_free_stack_element(stackp);
1072
1073 }
1074
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001075 // If we reached this, we didn't find anything downwards.
1076 // Let's check if we should do an upward search.
1077 if (search_ctx->ffsc_start_dir
1078 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
1079 {
1080 ff_stack_T *sptr;
zeertzjqe6ab23b2024-07-11 22:22:26 +02001081 // path_end may point to the NUL or the previous path separator
1082 int plen = (path_end - search_ctx->ffsc_start_dir)
1083 + (*path_end != NUL);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001084
1085 // is the last starting directory in the stop list?
1086 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
zeertzjqe6ab23b2024-07-11 22:22:26 +02001087 plen, search_ctx->ffsc_stopdirs_v) == TRUE)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001088 break;
1089
1090 // cut of last dir
1091 while (path_end > search_ctx->ffsc_start_dir
1092 && vim_ispathsep(*path_end))
1093 path_end--;
1094 while (path_end > search_ctx->ffsc_start_dir
1095 && !vim_ispathsep(path_end[-1]))
1096 path_end--;
1097 *path_end = 0;
1098 path_end--;
1099
1100 if (*search_ctx->ffsc_start_dir == 0)
1101 break;
1102
1103 if (STRLEN(search_ctx->ffsc_start_dir) + 1
1104 + STRLEN(search_ctx->ffsc_fix_path) < MAXPATHL)
1105 {
1106 STRCPY(file_path, search_ctx->ffsc_start_dir);
1107 add_pathsep(file_path);
1108 STRCAT(file_path, search_ctx->ffsc_fix_path);
1109 }
1110 else
1111 goto fail;
1112
1113 // create a new stack entry
1114 sptr = ff_create_stack_element(file_path,
1115 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
1116 if (sptr == NULL)
1117 break;
1118 ff_push(search_ctx, sptr);
1119 }
1120 else
1121 break;
1122 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001123
1124fail:
1125 vim_free(file_path);
1126 return NULL;
1127}
1128
1129/*
1130 * Free the list of lists of visited files and directories
1131 * Can handle it if the passed search_context is NULL;
1132 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001133 static void
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001134vim_findfile_free_visited(void *search_ctx_arg)
1135{
1136 ff_search_ctx_T *search_ctx;
1137
1138 if (search_ctx_arg == NULL)
1139 return;
1140
1141 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
1142 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
1143 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
1144}
1145
1146 static void
1147vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp)
1148{
1149 ff_visited_list_hdr_T *vp;
1150
1151 while (*list_headp != NULL)
1152 {
1153 vp = (*list_headp)->ffvl_next;
1154 ff_free_visited_list((*list_headp)->ffvl_visited_list);
1155
1156 vim_free((*list_headp)->ffvl_filename);
1157 vim_free(*list_headp);
1158 *list_headp = vp;
1159 }
1160 *list_headp = NULL;
1161}
1162
1163 static void
1164ff_free_visited_list(ff_visited_T *vl)
1165{
1166 ff_visited_T *vp;
1167
1168 while (vl != NULL)
1169 {
1170 vp = vl->ffv_next;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001171 vim_free(vl->ffv_wc_path);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001172 vim_free(vl);
1173 vl = vp;
1174 }
1175 vl = NULL;
1176}
1177
1178/*
1179 * Returns the already visited list for the given filename. If none is found it
1180 * allocates a new one.
1181 */
1182 static ff_visited_list_hdr_T*
1183ff_get_visited_list(
1184 char_u *filename,
1185 ff_visited_list_hdr_T **list_headp)
1186{
1187 ff_visited_list_hdr_T *retptr = NULL;
1188
1189 // check if a visited list for the given filename exists
1190 if (*list_headp != NULL)
1191 {
1192 retptr = *list_headp;
1193 while (retptr != NULL)
1194 {
1195 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
1196 {
1197#ifdef FF_VERBOSE
1198 if (p_verbose >= 5)
1199 {
1200 verbose_enter_scroll();
1201 smsg("ff_get_visited_list: FOUND list for %s",
1202 filename);
1203 // don't overwrite this either
1204 msg_puts("\n");
1205 verbose_leave_scroll();
1206 }
1207#endif
1208 return retptr;
1209 }
1210 retptr = retptr->ffvl_next;
1211 }
1212 }
1213
1214#ifdef FF_VERBOSE
1215 if (p_verbose >= 5)
1216 {
1217 verbose_enter_scroll();
1218 smsg("ff_get_visited_list: new list for %s", filename);
1219 // don't overwrite this either
1220 msg_puts("\n");
1221 verbose_leave_scroll();
1222 }
1223#endif
1224
1225 /*
1226 * if we reach this we didn't find a list and we have to allocate new list
1227 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001228 retptr = ALLOC_ONE(ff_visited_list_hdr_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001229 if (retptr == NULL)
1230 return NULL;
1231
1232 retptr->ffvl_visited_list = NULL;
1233 retptr->ffvl_filename = vim_strsave(filename);
1234 if (retptr->ffvl_filename == NULL)
1235 {
1236 vim_free(retptr);
1237 return NULL;
1238 }
1239 retptr->ffvl_next = *list_headp;
1240 *list_headp = retptr;
1241
1242 return retptr;
1243}
1244
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001245/*
1246 * check if two wildcard paths are equal. Returns TRUE or FALSE.
1247 * They are equal if:
1248 * - both paths are NULL
1249 * - they have the same length
1250 * - char by char comparison is OK
1251 * - the only differences are in the counters behind a '**', so
1252 * '**\20' is equal to '**\24'
1253 */
1254 static int
1255ff_wc_equal(char_u *s1, char_u *s2)
1256{
1257 int i, j;
1258 int c1 = NUL;
1259 int c2 = NUL;
1260 int prev1 = NUL;
1261 int prev2 = NUL;
1262
1263 if (s1 == s2)
1264 return TRUE;
1265
1266 if (s1 == NULL || s2 == NULL)
1267 return FALSE;
1268
1269 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
1270 {
1271 c1 = PTR2CHAR(s1 + i);
1272 c2 = PTR2CHAR(s2 + j);
1273
1274 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
1275 && (prev1 != '*' || prev2 != '*'))
1276 return FALSE;
1277 prev2 = prev1;
1278 prev1 = c1;
1279
Bram Moolenaar1614a142019-10-06 22:00:13 +02001280 i += mb_ptr2len(s1 + i);
1281 j += mb_ptr2len(s2 + j);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001282 }
1283 return s1[i] == s2[j];
1284}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001285
1286/*
1287 * maintains the list of already visited files and dirs
1288 * returns FAIL if the given file/dir is already in the list
1289 * returns OK if it is newly added
1290 *
1291 * TODO: What to do on memory allocation problems?
1292 * -> return TRUE - Better the file is found several times instead of
1293 * never.
1294 */
1295 static int
1296ff_check_visited(
1297 ff_visited_T **visited_list,
Bram Moolenaar2bd9dbc2022-08-25 18:12:06 +01001298 char_u *fname,
1299 char_u *wc_path)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001300{
1301 ff_visited_T *vp;
1302#ifdef UNIX
1303 stat_T st;
1304 int url = FALSE;
1305#endif
1306
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001307 // For a URL we only compare the name, otherwise we compare the
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001308 // device/inode (unix) or the full path name (not Unix).
1309 if (path_with_url(fname))
1310 {
1311 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
1312#ifdef UNIX
1313 url = TRUE;
1314#endif
1315 }
1316 else
1317 {
1318 ff_expand_buffer[0] = NUL;
1319#ifdef UNIX
1320 if (mch_stat((char *)fname, &st) < 0)
1321#else
1322 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
1323#endif
1324 return FAIL;
1325 }
1326
1327 // check against list of already visited files
1328 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
1329 {
1330 if (
1331#ifdef UNIX
1332 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
1333 && vp->ffv_ino == st.st_ino)
1334 :
1335#endif
1336 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
1337 )
1338 {
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001339 // are the wildcard parts equal
1340 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001341 // already visited
1342 return FAIL;
1343 }
1344 }
1345
1346 /*
1347 * New file/dir. Add it to the list of visited files/dirs.
1348 */
zeertzjq1b438a82023-02-01 13:11:15 +00001349 vp = alloc(
1350 offsetof(ff_visited_T, ffv_fname) + STRLEN(ff_expand_buffer) + 1);
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001351 if (vp == NULL)
1352 return OK;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001353
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001354#ifdef UNIX
1355 if (!url)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001356 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001357 vp->ffv_dev_valid = TRUE;
1358 vp->ffv_ino = st.st_ino;
1359 vp->ffv_dev = st.st_dev;
1360 vp->ffv_fname[0] = NUL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001361 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001362 else
1363 {
1364 vp->ffv_dev_valid = FALSE;
1365#endif
1366 STRCPY(vp->ffv_fname, ff_expand_buffer);
1367#ifdef UNIX
1368 }
1369#endif
1370 if (wc_path != NULL)
1371 vp->ffv_wc_path = vim_strsave(wc_path);
1372 else
1373 vp->ffv_wc_path = NULL;
1374
1375 vp->ffv_next = *visited_list;
1376 *visited_list = vp;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001377
1378 return OK;
1379}
1380
1381/*
1382 * create stack element from given path pieces
1383 */
1384 static ff_stack_T *
1385ff_create_stack_element(
1386 char_u *fix_part,
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001387 char_u *wc_part,
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001388 int level,
1389 int star_star_empty)
1390{
1391 ff_stack_T *new;
1392
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001393 new = ALLOC_ONE(ff_stack_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001394 if (new == NULL)
1395 return NULL;
1396
1397 new->ffs_prev = NULL;
1398 new->ffs_filearray = NULL;
1399 new->ffs_filearray_size = 0;
1400 new->ffs_filearray_cur = 0;
1401 new->ffs_stage = 0;
1402 new->ffs_level = level;
1403 new->ffs_star_star_empty = star_star_empty;
1404
1405 // the following saves NULL pointer checks in vim_findfile
1406 if (fix_part == NULL)
1407 fix_part = (char_u *)"";
1408 new->ffs_fix_path = vim_strsave(fix_part);
1409
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001410 if (wc_part == NULL)
1411 wc_part = (char_u *)"";
1412 new->ffs_wc_path = vim_strsave(wc_part);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001413
Bram Moolenaar2bd9dbc2022-08-25 18:12:06 +01001414 if (new->ffs_fix_path == NULL || new->ffs_wc_path == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001415 {
1416 ff_free_stack_element(new);
1417 new = NULL;
1418 }
1419
1420 return new;
1421}
1422
1423/*
1424 * Push a dir on the directory stack.
1425 */
1426 static void
1427ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)
1428{
1429 // check for NULL pointer, not to return an error to the user, but
1430 // to prevent a crash
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001431 if (stack_ptr == NULL)
1432 return;
1433
1434 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
1435 search_ctx->ffsc_stack_ptr = stack_ptr;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001436}
1437
1438/*
1439 * Pop a dir from the directory stack.
1440 * Returns NULL if stack is empty.
1441 */
1442 static ff_stack_T *
1443ff_pop(ff_search_ctx_T *search_ctx)
1444{
1445 ff_stack_T *sptr;
1446
1447 sptr = search_ctx->ffsc_stack_ptr;
1448 if (search_ctx->ffsc_stack_ptr != NULL)
1449 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
1450
1451 return sptr;
1452}
1453
1454/*
1455 * free the given stack element
1456 */
1457 static void
1458ff_free_stack_element(ff_stack_T *stack_ptr)
1459{
1460 // vim_free handles possible NULL pointers
1461 vim_free(stack_ptr->ffs_fix_path);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001462 vim_free(stack_ptr->ffs_wc_path);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001463
1464 if (stack_ptr->ffs_filearray != NULL)
1465 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
1466
1467 vim_free(stack_ptr);
1468}
1469
1470/*
1471 * Clear the search context, but NOT the visited list.
1472 */
1473 static void
1474ff_clear(ff_search_ctx_T *search_ctx)
1475{
1476 ff_stack_T *sptr;
1477
1478 // clear up stack
1479 while ((sptr = ff_pop(search_ctx)) != NULL)
1480 ff_free_stack_element(sptr);
1481
1482 vim_free(search_ctx->ffsc_file_to_search);
1483 vim_free(search_ctx->ffsc_start_dir);
1484 vim_free(search_ctx->ffsc_fix_path);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001485 vim_free(search_ctx->ffsc_wc_path);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001486
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001487 if (search_ctx->ffsc_stopdirs_v != NULL)
1488 {
1489 int i = 0;
1490
1491 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
1492 {
1493 vim_free(search_ctx->ffsc_stopdirs_v[i]);
1494 i++;
1495 }
1496 vim_free(search_ctx->ffsc_stopdirs_v);
1497 }
1498 search_ctx->ffsc_stopdirs_v = NULL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001499
1500 // reset everything
1501 search_ctx->ffsc_file_to_search = NULL;
1502 search_ctx->ffsc_start_dir = NULL;
1503 search_ctx->ffsc_fix_path = NULL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001504 search_ctx->ffsc_wc_path = NULL;
1505 search_ctx->ffsc_level = 0;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001506}
1507
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001508/*
1509 * check if the given path is in the stopdirs
1510 * returns TRUE if yes else FALSE
1511 */
1512 static int
1513ff_path_in_stoplist(char_u *path, int path_len, char_u **stopdirs_v)
1514{
1515 int i = 0;
1516
1517 // eat up trailing path separators, except the first
1518 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
1519 path_len--;
1520
1521 // if no path consider it as match
1522 if (path_len == 0)
1523 return TRUE;
1524
1525 for (i = 0; stopdirs_v[i] != NULL; i++)
zeertzjqe6ab23b2024-07-11 22:22:26 +02001526 // match for parent directory. So '/home' also matches
1527 // '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
1528 // '/home/r' would also match '/home/rks'
1529 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
1530 && ((int)STRLEN(stopdirs_v[i]) <= path_len
1531 || vim_ispathsep(stopdirs_v[i][path_len])))
1532 return TRUE;
1533
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001534 return FALSE;
1535}
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001536
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001537/*
1538 * Find the file name "ptr[len]" in the path. Also finds directory names.
1539 *
1540 * On the first call set the parameter 'first' to TRUE to initialize
1541 * the search. For repeating calls to FALSE.
1542 *
1543 * Repeating calls will return other files called 'ptr[len]' from the path.
1544 *
1545 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
1546 * don't need valid values.
1547 *
1548 * If nothing found on the first call the option FNAME_MESS will issue the
1549 * message:
1550 * 'Can't find file "<file>" in path'
1551 * On repeating calls:
1552 * 'No more file "<file>" found in path'
1553 *
1554 * options:
1555 * FNAME_MESS give error message when not found
1556 *
1557 * Uses NameBuff[]!
1558 *
1559 * Returns an allocated string for the file name. NULL for error.
1560 *
1561 */
1562 char_u *
1563find_file_in_path(
1564 char_u *ptr, // file name
1565 int len, // length of file name
1566 int options,
1567 int first, // use count'th matching file name
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001568 char_u *rel_fname, // file name searching relative to
1569 char_u **file_to_find, // in/out: modified copy of file name
1570 char **search_ctx) // in/out: state of the search
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001571{
1572 return find_file_in_path_option(ptr, len, options, first,
1573 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001574 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua,
1575 file_to_find, search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001576}
1577
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001578# if defined(EXITFREE) || defined(PROTO)
1579 void
1580free_findfile(void)
1581{
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001582 VIM_CLEAR(ff_expand_buffer);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001583}
1584# endif
1585
1586/*
1587 * Find the directory name "ptr[len]" in the path.
1588 *
1589 * options:
1590 * FNAME_MESS give error message when not found
1591 * FNAME_UNESC unescape backslashes.
1592 *
1593 * Uses NameBuff[]!
1594 *
1595 * Returns an allocated string for the file name. NULL for error.
1596 */
1597 char_u *
1598find_directory_in_path(
1599 char_u *ptr, // file name
1600 int len, // length of file name
1601 int options,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001602 char_u *rel_fname, // file name searching relative to
1603 char_u **file_to_find, // in/out: modified copy of file name
1604 char **search_ctx) // in/out: state of the search
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001605{
1606 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001607 FINDFILE_DIR, rel_fname, (char_u *)"",
1608 file_to_find, search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001609}
1610
1611 char_u *
1612find_file_in_path_option(
1613 char_u *ptr, // file name
1614 int len, // length of file name
1615 int options,
1616 int first, // use count'th matching file name
1617 char_u *path_option, // p_path or p_cdpath
1618 int find_what, // FINDFILE_FILE, _DIR or _BOTH
1619 char_u *rel_fname, // file name we are looking relative to.
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001620 char_u *suffixes, // list of suffixes, 'suffixesadd' option
1621 char_u **file_to_find, // in/out: modified copy of file name
1622 char **search_ctx_arg) // in/out: state of the search
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001623{
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001624 ff_search_ctx_T **search_ctx = (ff_search_ctx_T **)search_ctx_arg;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001625 static char_u *dir;
1626 static int did_findfile_init = FALSE;
1627 char_u save_char;
1628 char_u *file_name = NULL;
1629 char_u *buf = NULL;
1630 int rel_to_curdir;
1631# ifdef AMIGA
1632 struct Process *proc = (struct Process *)FindTask(0L);
1633 APTR save_winptr = proc->pr_WindowPtr;
1634
1635 // Avoid a requester here for a volume that doesn't exist.
1636 proc->pr_WindowPtr = (APTR)-1L;
1637# endif
1638
1639 if (first == TRUE)
1640 {
Bram Moolenaare015d992021-11-17 19:01:53 +00001641 if (len == 0)
1642 return NULL;
1643
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001644 // copy file name into NameBuff, expanding environment variables
1645 save_char = ptr[len];
1646 ptr[len] = NUL;
1647 expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
1648 ptr[len] = save_char;
1649
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001650 vim_free(*file_to_find);
1651 *file_to_find = vim_strsave(NameBuff);
1652 if (*file_to_find == NULL) // out of memory
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001653 {
1654 file_name = NULL;
1655 goto theend;
1656 }
1657 if (options & FNAME_UNESC)
1658 {
1659 // Change all "\ " to " ".
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001660 for (ptr = *file_to_find; *ptr != NUL; ++ptr)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001661 if (ptr[0] == '\\' && ptr[1] == ' ')
1662 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
1663 }
1664 }
1665
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001666 rel_to_curdir = ((*file_to_find)[0] == '.'
1667 && ((*file_to_find)[1] == NUL
1668 || vim_ispathsep((*file_to_find)[1])
1669 || ((*file_to_find)[1] == '.'
1670 && ((*file_to_find)[2] == NUL
1671 || vim_ispathsep((*file_to_find)[2])))));
1672 if (vim_isAbsName(*file_to_find)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001673 // "..", "../path", "." and "./path": don't use the path_option
1674 || rel_to_curdir
1675# if defined(MSWIN)
1676 // handle "\tmp" as absolute path
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001677 || vim_ispathsep((*file_to_find)[0])
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001678 // handle "c:name" as absolute path
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001679 || ((*file_to_find)[0] != NUL && (*file_to_find)[1] == ':')
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001680# endif
1681# ifdef AMIGA
1682 // handle ":tmp" as absolute path
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001683 || (*file_to_find)[0] == ':'
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001684# endif
1685 )
1686 {
1687 /*
1688 * Absolute path, no need to use "path_option".
1689 * If this is not a first call, return NULL. We already returned a
1690 * filename on the first call.
1691 */
1692 if (first == TRUE)
1693 {
1694 int l;
1695 int run;
1696
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001697 if (path_with_url(*file_to_find))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001698 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001699 file_name = vim_strsave(*file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001700 goto theend;
1701 }
1702
1703 // When FNAME_REL flag given first use the directory of the file.
1704 // Otherwise or when this fails use the current directory.
1705 for (run = 1; run <= 2; ++run)
1706 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001707 l = (int)STRLEN(*file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001708 if (run == 1
1709 && rel_to_curdir
1710 && (options & FNAME_REL)
1711 && rel_fname != NULL
1712 && STRLEN(rel_fname) + l < MAXPATHL)
1713 {
1714 STRCPY(NameBuff, rel_fname);
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001715 STRCPY(gettail(NameBuff), *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001716 l = (int)STRLEN(NameBuff);
1717 }
1718 else
1719 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001720 STRCPY(NameBuff, *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001721 run = 2;
1722 }
1723
1724 // When the file doesn't exist, try adding parts of
1725 // 'suffixesadd'.
1726 buf = suffixes;
1727 for (;;)
1728 {
1729 if (mch_getperm(NameBuff) >= 0
1730 && (find_what == FINDFILE_BOTH
1731 || ((find_what == FINDFILE_DIR)
1732 == mch_isdir(NameBuff))))
1733 {
1734 file_name = vim_strsave(NameBuff);
1735 goto theend;
1736 }
1737 if (*buf == NUL)
1738 break;
1739 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
1740 }
1741 }
1742 }
1743 }
1744 else
1745 {
1746 /*
1747 * Loop over all paths in the 'path' or 'cdpath' option.
1748 * When "first" is set, first setup to the start of the option.
1749 * Otherwise continue to find the next match.
1750 */
1751 if (first == TRUE)
1752 {
1753 // vim_findfile_free_visited can handle a possible NULL pointer
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001754 vim_findfile_free_visited(*search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001755 dir = path_option;
1756 did_findfile_init = FALSE;
1757 }
1758
1759 for (;;)
1760 {
1761 if (did_findfile_init)
1762 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001763 file_name = vim_findfile(*search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001764 if (file_name != NULL)
1765 break;
1766
1767 did_findfile_init = FALSE;
1768 }
1769 else
1770 {
1771 char_u *r_ptr;
1772
1773 if (dir == NULL || *dir == NUL)
1774 {
1775 // We searched all paths of the option, now we can
1776 // free the search context.
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001777 vim_findfile_cleanup(*search_ctx);
1778 *search_ctx = NULL;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001779 break;
1780 }
1781
Bram Moolenaar51e14382019-05-25 20:21:28 +02001782 if ((buf = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001783 break;
1784
1785 // copy next path
1786 buf[0] = 0;
1787 copy_option_part(&dir, buf, MAXPATHL, " ,");
1788
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001789 // get the stopdir string
1790 r_ptr = vim_findfile_stopdir(buf);
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001791 *search_ctx = vim_findfile_init(buf, *file_to_find,
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001792 r_ptr, 100, FALSE, find_what,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001793 *search_ctx, FALSE, rel_fname);
1794 if (*search_ctx != NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001795 did_findfile_init = TRUE;
1796 vim_free(buf);
1797 }
1798 }
1799 }
1800 if (file_name == NULL && (options & FNAME_MESS))
1801 {
1802 if (first == TRUE)
1803 {
1804 if (find_what == FINDFILE_DIR)
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001805 semsg(_(e_cant_find_directory_str_in_cdpath), *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001806 else
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001807 semsg(_(e_cant_find_file_str_in_path), *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001808 }
1809 else
1810 {
1811 if (find_what == FINDFILE_DIR)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001812 semsg(_(e_no_more_directory_str_found_in_cdpath),
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001813 *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001814 else
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00001815 semsg(_(e_no_more_file_str_found_in_path), *file_to_find);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001816 }
1817 }
1818
1819theend:
1820# ifdef AMIGA
1821 proc->pr_WindowPtr = save_winptr;
1822# endif
1823 return file_name;
1824}
1825
1826/*
1827 * Get the file name at the cursor.
1828 * If Visual mode is active, use the selected text if it's in one line.
1829 * Returns the name in allocated memory, NULL for failure.
1830 */
1831 char_u *
1832grab_file_name(long count, linenr_T *file_lnum)
1833{
1834 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
1835
1836 if (VIsual_active)
1837 {
1838 int len;
1839 char_u *ptr;
1840
1841 if (get_visual_text(NULL, &ptr, &len) == FAIL)
1842 return NULL;
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +02001843 // Only recognize ":123" here
Keith Thompson184f71c2024-01-04 21:19:04 +01001844 if (file_lnum != NULL && ptr[len] == ':' && SAFE_isdigit(ptr[len + 1]))
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +02001845 {
1846 char_u *p = ptr + len + 1;
1847
1848 *file_lnum = getdigits(&p);
1849 }
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001850 return find_file_name_in_path(ptr, len, options,
1851 count, curbuf->b_ffname);
1852 }
1853 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
1854}
1855
1856/*
1857 * Return the file name under or after the cursor.
1858 *
1859 * The 'path' option is searched if the file name is not absolute.
1860 * The string returned has been alloc'ed and should be freed by the caller.
1861 * NULL is returned if the file name or file is not found.
1862 *
1863 * options:
1864 * FNAME_MESS give error messages
1865 * FNAME_EXP expand to path
1866 * FNAME_HYP check for hypertext link
1867 * FNAME_INCL apply "includeexpr"
1868 */
1869 char_u *
1870file_name_at_cursor(int options, long count, linenr_T *file_lnum)
1871{
1872 return file_name_in_line(ml_get_curline(),
1873 curwin->w_cursor.col, options, count, curbuf->b_ffname,
1874 file_lnum);
1875}
1876
1877/*
1878 * Return the name of the file under or after ptr[col].
1879 * Otherwise like file_name_at_cursor().
1880 */
1881 char_u *
1882file_name_in_line(
1883 char_u *line,
1884 int col,
1885 int options,
1886 long count,
1887 char_u *rel_fname, // file we are searching relative to
1888 linenr_T *file_lnum) // line number after the file name
1889{
1890 char_u *ptr;
1891 int len;
1892 int in_type = TRUE;
1893 int is_url = FALSE;
1894
1895 /*
1896 * search forward for what could be the start of a file name
1897 */
1898 ptr = line + col;
1899 while (*ptr != NUL && !vim_isfilec(*ptr))
1900 MB_PTR_ADV(ptr);
1901 if (*ptr == NUL) // nothing found
1902 {
1903 if (options & FNAME_MESS)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001904 emsg(_(e_no_file_name_under_cursor));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001905 return NULL;
1906 }
1907
1908 /*
1909 * Search backward for first char of the file name.
1910 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
1911 */
1912 while (ptr > line)
1913 {
1914 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
1915 ptr -= len + 1;
1916 else if (vim_isfilec(ptr[-1])
1917 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
1918 --ptr;
1919 else
1920 break;
1921 }
1922
1923 /*
1924 * Search forward for the last char of the file name.
1925 * Also allow "://" when ':' is not in 'isfname'.
1926 */
1927 len = 0;
1928 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar747f1102022-09-18 13:06:41 +01001929 || ((options & FNAME_HYP) && path_is_url(ptr + len))
1930 || (is_url && vim_strchr((char_u *)":?&=", ptr[len]) != NULL))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001931 {
Bram Moolenaar747f1102022-09-18 13:06:41 +01001932 // After type:// we also include :, ?, & and = as valid characters, so
1933 // that http://google.com:8080?q=this&that=ok works.
1934 if ((ptr[len] >= 'A' && ptr[len] <= 'Z')
1935 || (ptr[len] >= 'a' && ptr[len] <= 'z'))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001936 {
1937 if (in_type && path_is_url(ptr + len + 1))
1938 is_url = TRUE;
1939 }
1940 else
1941 in_type = FALSE;
1942
1943 if (ptr[len] == '\\')
1944 // Skip over the "\" in "\ ".
1945 ++len;
1946 if (has_mbyte)
1947 len += (*mb_ptr2len)(ptr + len);
1948 else
1949 ++len;
1950 }
1951
1952 /*
1953 * If there is trailing punctuation, remove it.
1954 * But don't remove "..", could be a directory name.
1955 */
1956 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
1957 && ptr[len - 2] != '.')
1958 --len;
1959
1960 if (file_lnum != NULL)
1961 {
1962 char_u *p;
Bram Moolenaar64e74c92019-12-22 15:38:06 +01001963 char *line_english = " line ";
1964 char *line_transl = _(line_msg);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001965
Bram Moolenaar64e74c92019-12-22 15:38:06 +01001966 // Get the number after the file name and a separator character.
1967 // Also accept " line 999" with and without the same translation as
1968 // used in last_set_msg().
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001969 p = ptr + len;
Bram Moolenaar64e74c92019-12-22 15:38:06 +01001970 if (STRNCMP(p, line_english, STRLEN(line_english)) == 0)
1971 p += STRLEN(line_english);
1972 else if (STRNCMP(p, line_transl, STRLEN(line_transl)) == 0)
1973 p += STRLEN(line_transl);
1974 else
1975 p = skipwhite(p);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001976 if (*p != NUL)
1977 {
Keith Thompson184f71c2024-01-04 21:19:04 +01001978 if (!SAFE_isdigit(*p))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001979 ++p; // skip the separator
1980 p = skipwhite(p);
Keith Thompson184f71c2024-01-04 21:19:04 +01001981 if (SAFE_isdigit(*p))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001982 *file_lnum = (int)getdigits(&p);
1983 }
1984 }
1985
1986 return find_file_name_in_path(ptr, len, options, count, rel_fname);
1987}
1988
1989# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
1990 static char_u *
1991eval_includeexpr(char_u *ptr, int len)
1992{
1993 char_u *res;
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001994 sctx_T save_sctx = current_sctx;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001995
1996 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00001997 current_sctx = curbuf->b_p_script_ctx[BV_INEX];
1998
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001999 res = eval_to_string_safe(curbuf->b_p_inex,
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002000 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL),
2001 TRUE, TRUE);
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002002
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002003 set_vim_var_string(VV_FNAME, NULL, 0);
Bram Moolenaar47bcc5f2022-01-22 20:19:22 +00002004 current_sctx = save_sctx;
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002005 return res;
2006}
2007# endif
2008
2009/*
2010 * Return the name of the file ptr[len] in 'path'.
2011 * Otherwise like file_name_at_cursor().
2012 */
2013 char_u *
2014find_file_name_in_path(
2015 char_u *ptr,
2016 int len,
2017 int options,
2018 long count,
2019 char_u *rel_fname) // file we are searching relative to
2020{
2021 char_u *file_name;
2022 int c;
2023# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2024 char_u *tofree = NULL;
Bram Moolenaar615ddd52021-11-17 18:00:31 +00002025# endif
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002026
Bram Moolenaar615ddd52021-11-17 18:00:31 +00002027 if (len == 0)
2028 return NULL;
2029
2030# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002031 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
2032 {
2033 tofree = eval_includeexpr(ptr, len);
2034 if (tofree != NULL)
2035 {
2036 ptr = tofree;
2037 len = (int)STRLEN(ptr);
2038 }
2039 }
2040# endif
2041
2042 if (options & FNAME_EXP)
2043 {
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002044 char_u *file_to_find = NULL;
2045 char *search_ctx = NULL;
2046
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002047 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002048 TRUE, rel_fname, &file_to_find, &search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002049
2050# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2051 /*
2052 * If the file could not be found in a normal way, try applying
2053 * 'includeexpr' (unless done already).
2054 */
2055 if (file_name == NULL
2056 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
2057 {
2058 tofree = eval_includeexpr(ptr, len);
2059 if (tofree != NULL)
2060 {
2061 ptr = tofree;
2062 len = (int)STRLEN(ptr);
2063 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002064 TRUE, rel_fname, &file_to_find, &search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002065 }
2066 }
2067# endif
2068 if (file_name == NULL && (options & FNAME_MESS))
2069 {
2070 c = ptr[len];
2071 ptr[len] = NUL;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002072 semsg(_(e_cant_find_file_str_in_path_2), ptr);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002073 ptr[len] = c;
2074 }
2075
2076 // Repeat finding the file "count" times. This matters when it
2077 // appears several times in the path.
2078 while (file_name != NULL && --count > 0)
2079 {
2080 vim_free(file_name);
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002081 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname,
2082 &file_to_find, &search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002083 }
Bram Moolenaar5145c9a2023-03-11 13:55:53 +00002084
2085 vim_free(file_to_find);
2086 vim_findfile_cleanup(search_ctx);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002087 }
2088 else
2089 file_name = vim_strnsave(ptr, len);
2090
2091# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2092 vim_free(tofree);
2093# endif
2094
2095 return file_name;
2096}
2097
2098/*
2099 * Return the end of the directory name, on the first path
2100 * separator:
2101 * "/path/file", "/path/dir/", "/path//dir", "/file"
2102 * ^ ^ ^ ^
2103 */
2104 static char_u *
2105gettail_dir(char_u *fname)
2106{
2107 char_u *dir_end = fname;
2108 char_u *next_dir_end = fname;
2109 int look_for_sep = TRUE;
2110 char_u *p;
2111
2112 for (p = fname; *p != NUL; )
2113 {
2114 if (vim_ispathsep(*p))
2115 {
2116 if (look_for_sep)
2117 {
2118 next_dir_end = p;
2119 look_for_sep = FALSE;
2120 }
2121 }
2122 else
2123 {
2124 if (!look_for_sep)
2125 dir_end = next_dir_end;
2126 look_for_sep = TRUE;
2127 }
2128 MB_PTR_ADV(p);
2129 }
2130 return dir_end;
2131}
2132
2133/*
2134 * return TRUE if 'c' is a path list separator.
2135 */
2136 int
2137vim_ispathlistsep(int c)
2138{
2139# ifdef UNIX
2140 return (c == ':');
2141# else
2142 return (c == ';'); // might not be right for every system...
2143# endif
2144}
2145
2146/*
2147 * Moves "*psep" back to the previous path separator in "path".
2148 * Returns FAIL is "*psep" ends up at the beginning of "path".
2149 */
2150 static int
2151find_previous_pathsep(char_u *path, char_u **psep)
2152{
2153 // skip the current separator
2154 if (*psep > path && vim_ispathsep(**psep))
2155 --*psep;
2156
2157 // find the previous separator
2158 while (*psep > path)
2159 {
2160 if (vim_ispathsep(**psep))
2161 return OK;
2162 MB_PTR_BACK(path, *psep);
2163 }
2164
2165 return FAIL;
2166}
2167
2168/*
2169 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
2170 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
2171 */
2172 static int
2173is_unique(char_u *maybe_unique, garray_T *gap, int i)
2174{
2175 int j;
2176 int candidate_len;
2177 int other_path_len;
2178 char_u **other_paths = (char_u **)gap->ga_data;
2179 char_u *rival;
2180
2181 for (j = 0; j < gap->ga_len; j++)
2182 {
2183 if (j == i)
2184 continue; // don't compare it with itself
2185
2186 candidate_len = (int)STRLEN(maybe_unique);
2187 other_path_len = (int)STRLEN(other_paths[j]);
2188 if (other_path_len < candidate_len)
2189 continue; // it's different when it's shorter
2190
2191 rival = other_paths[j] + other_path_len - candidate_len;
2192 if (fnamecmp(maybe_unique, rival) == 0
2193 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
2194 return FALSE; // match
2195 }
2196
2197 return TRUE; // no match found
2198}
2199
2200/*
2201 * Split the 'path' option into an array of strings in garray_T. Relative
2202 * paths are expanded to their equivalent fullpath. This includes the "."
2203 * (relative to current buffer directory) and empty path (relative to current
2204 * directory) notations.
2205 *
2206 * TODO: handle upward search (;) and path limiter (**N) notations by
2207 * expanding each into their equivalent path(s).
2208 */
2209 static void
2210expand_path_option(char_u *curdir, garray_T *gap)
2211{
2212 char_u *path_option = *curbuf->b_p_path == NUL
2213 ? p_path : curbuf->b_p_path;
2214 char_u *buf;
2215 char_u *p;
2216 int len;
2217
Bram Moolenaar51e14382019-05-25 20:21:28 +02002218 if ((buf = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002219 return;
2220
2221 while (*path_option != NUL)
2222 {
2223 copy_option_part(&path_option, buf, MAXPATHL, " ,");
2224
2225 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
2226 {
2227 // Relative to current buffer:
2228 // "/path/file" + "." -> "/path/"
2229 // "/path/file" + "./subdir" -> "/path/subdir"
2230 if (curbuf->b_ffname == NULL)
2231 continue;
2232 p = gettail(curbuf->b_ffname);
2233 len = (int)(p - curbuf->b_ffname);
2234 if (len + (int)STRLEN(buf) >= MAXPATHL)
2235 continue;
2236 if (buf[1] == NUL)
2237 buf[len] = NUL;
2238 else
2239 STRMOVE(buf + len, buf + 2);
2240 mch_memmove(buf, curbuf->b_ffname, len);
2241 simplify_filename(buf);
2242 }
2243 else if (buf[0] == NUL)
2244 // relative to current directory
2245 STRCPY(buf, curdir);
2246 else if (path_with_url(buf))
2247 // URL can't be used here
2248 continue;
2249 else if (!mch_isFullName(buf))
2250 {
2251 // Expand relative path to their full path equivalent
2252 len = (int)STRLEN(curdir);
2253 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
2254 continue;
2255 STRMOVE(buf + len + 1, buf);
2256 STRCPY(buf, curdir);
2257 buf[len] = PATHSEP;
2258 simplify_filename(buf);
2259 }
2260
2261 if (ga_grow(gap, 1) == FAIL)
2262 break;
2263
2264# if defined(MSWIN)
2265 // Avoid the path ending in a backslash, it fails when a comma is
2266 // appended.
2267 len = (int)STRLEN(buf);
2268 if (buf[len - 1] == '\\')
2269 buf[len - 1] = '/';
2270# endif
2271
2272 p = vim_strsave(buf);
2273 if (p == NULL)
2274 break;
2275 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
2276 }
2277
2278 vim_free(buf);
2279}
2280
2281/*
2282 * Returns a pointer to the file or directory name in "fname" that matches the
2283 * longest path in "ga"p, or NULL if there is no match. For example:
2284 *
2285 * path: /foo/bar/baz
2286 * fname: /foo/bar/baz/quux.txt
2287 * returns: ^this
2288 */
2289 static char_u *
2290get_path_cutoff(char_u *fname, garray_T *gap)
2291{
2292 int i;
2293 int maxlen = 0;
2294 char_u **path_part = (char_u **)gap->ga_data;
2295 char_u *cutoff = NULL;
2296
2297 for (i = 0; i < gap->ga_len; i++)
2298 {
2299 int j = 0;
2300
2301 while ((fname[j] == path_part[i][j]
2302# if defined(MSWIN)
2303 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
2304# endif
2305 ) && fname[j] != NUL && path_part[i][j] != NUL)
2306 j++;
2307 if (j > maxlen)
2308 {
2309 maxlen = j;
2310 cutoff = &fname[j];
2311 }
2312 }
2313
2314 // skip to the file or directory name
2315 if (cutoff != NULL)
2316 while (vim_ispathsep(*cutoff))
2317 MB_PTR_ADV(cutoff);
2318
2319 return cutoff;
2320}
2321
2322/*
2323 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
2324 * that they are unique with respect to each other while conserving the part
2325 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
2326 */
2327 void
2328uniquefy_paths(garray_T *gap, char_u *pattern)
2329{
2330 int i;
2331 int len;
2332 char_u **fnames = (char_u **)gap->ga_data;
2333 int sort_again = FALSE;
2334 char_u *pat;
2335 char_u *file_pattern;
2336 char_u *curdir;
2337 regmatch_T regmatch;
2338 garray_T path_ga;
2339 char_u **in_curdir = NULL;
2340 char_u *short_name;
2341
2342 remove_duplicates(gap);
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002343 ga_init2(&path_ga, sizeof(char_u *), 1);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002344
2345 /*
2346 * We need to prepend a '*' at the beginning of file_pattern so that the
2347 * regex matches anywhere in the path. FIXME: is this valid for all
2348 * possible patterns?
2349 */
2350 len = (int)STRLEN(pattern);
2351 file_pattern = alloc(len + 2);
2352 if (file_pattern == NULL)
2353 return;
2354 file_pattern[0] = '*';
2355 file_pattern[1] = NUL;
2356 STRCAT(file_pattern, pattern);
2357 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
2358 vim_free(file_pattern);
2359 if (pat == NULL)
2360 return;
2361
2362 regmatch.rm_ic = TRUE; // always ignore case
2363 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2364 vim_free(pat);
2365 if (regmatch.regprog == NULL)
2366 return;
2367
Bram Moolenaar51e14382019-05-25 20:21:28 +02002368 if ((curdir = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002369 goto theend;
2370 mch_dirname(curdir, MAXPATHL);
2371 expand_path_option(curdir, &path_ga);
2372
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002373 in_curdir = ALLOC_CLEAR_MULT(char_u *, gap->ga_len);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002374 if (in_curdir == NULL)
2375 goto theend;
2376
2377 for (i = 0; i < gap->ga_len && !got_int; i++)
2378 {
2379 char_u *path = fnames[i];
2380 int is_in_curdir;
2381 char_u *dir_end = gettail_dir(path);
2382 char_u *pathsep_p;
2383 char_u *path_cutoff;
2384
2385 len = (int)STRLEN(path);
2386 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
2387 && curdir[dir_end - path] == NUL;
2388 if (is_in_curdir)
2389 in_curdir[i] = vim_strsave(path);
2390
2391 // Shorten the filename while maintaining its uniqueness
2392 path_cutoff = get_path_cutoff(path, &path_ga);
2393
2394 // Don't assume all files can be reached without path when search
2395 // pattern starts with star star slash, so only remove path_cutoff
2396 // when possible.
2397 if (pattern[0] == '*' && pattern[1] == '*'
2398 && vim_ispathsep_nocolon(pattern[2])
2399 && path_cutoff != NULL
2400 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
2401 && is_unique(path_cutoff, gap, i))
2402 {
2403 sort_again = TRUE;
2404 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
2405 }
2406 else
2407 {
2408 // Here all files can be reached without path, so get shortest
2409 // unique path. We start at the end of the path.
2410 pathsep_p = path + len - 1;
2411
2412 while (find_previous_pathsep(path, &pathsep_p))
2413 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
2414 && is_unique(pathsep_p + 1, gap, i)
2415 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
2416 {
2417 sort_again = TRUE;
2418 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
2419 break;
2420 }
2421 }
2422
2423 if (mch_isFullName(path))
2424 {
2425 /*
2426 * Last resort: shorten relative to curdir if possible.
2427 * 'possible' means:
2428 * 1. It is under the current directory.
2429 * 2. The result is actually shorter than the original.
2430 *
2431 * Before curdir After
2432 * /foo/bar/file.txt /foo/bar ./file.txt
2433 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
2434 * /file.txt / /file.txt
2435 * c:\file.txt c:\ .\file.txt
2436 */
2437 short_name = shorten_fname(path, curdir);
2438 if (short_name != NULL && short_name > path + 1
2439# if defined(MSWIN)
2440 // On windows,
2441 // shorten_fname("c:\a\a.txt", "c:\a\b")
2442 // returns "\a\a.txt", which is not really the short
2443 // name, hence:
2444 && !vim_ispathsep(*short_name)
2445# endif
2446 )
2447 {
2448 STRCPY(path, ".");
2449 add_pathsep(path);
2450 STRMOVE(path + STRLEN(path), short_name);
2451 }
2452 }
2453 ui_breakcheck();
2454 }
2455
2456 // Shorten filenames in /in/current/directory/{filename}
2457 for (i = 0; i < gap->ga_len && !got_int; i++)
2458 {
2459 char_u *rel_path;
2460 char_u *path = in_curdir[i];
2461
2462 if (path == NULL)
2463 continue;
2464
2465 // If the {filename} is not unique, change it to ./{filename}.
2466 // Else reduce it to {filename}
2467 short_name = shorten_fname(path, curdir);
2468 if (short_name == NULL)
2469 short_name = path;
2470 if (is_unique(short_name, gap, i))
2471 {
2472 STRCPY(fnames[i], short_name);
2473 continue;
2474 }
2475
Bram Moolenaar51e14382019-05-25 20:21:28 +02002476 rel_path = alloc(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002477 if (rel_path == NULL)
2478 goto theend;
2479 STRCPY(rel_path, ".");
2480 add_pathsep(rel_path);
2481 STRCAT(rel_path, short_name);
2482
2483 vim_free(fnames[i]);
2484 fnames[i] = rel_path;
2485 sort_again = TRUE;
2486 ui_breakcheck();
2487 }
2488
2489theend:
2490 vim_free(curdir);
2491 if (in_curdir != NULL)
2492 {
2493 for (i = 0; i < gap->ga_len; i++)
2494 vim_free(in_curdir[i]);
2495 vim_free(in_curdir);
2496 }
2497 ga_clear_strings(&path_ga);
2498 vim_regfree(regmatch.regprog);
2499
2500 if (sort_again)
2501 remove_duplicates(gap);
2502}
2503
2504/*
2505 * Calls globpath() with 'path' values for the given pattern and stores the
2506 * result in "gap".
2507 * Returns the total number of matches.
2508 */
2509 int
2510expand_in_path(
2511 garray_T *gap,
2512 char_u *pattern,
2513 int flags) // EW_* flags
2514{
2515 char_u *curdir;
2516 garray_T path_ga;
2517 char_u *paths = NULL;
2518 int glob_flags = 0;
2519
Bram Moolenaar964b3742019-05-24 18:54:09 +02002520 if ((curdir = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002521 return 0;
2522 mch_dirname(curdir, MAXPATHL);
2523
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002524 ga_init2(&path_ga, sizeof(char_u *), 1);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002525 expand_path_option(curdir, &path_ga);
2526 vim_free(curdir);
2527 if (path_ga.ga_len == 0)
2528 return 0;
2529
2530 paths = ga_concat_strings(&path_ga, ",");
2531 ga_clear_strings(&path_ga);
2532 if (paths == NULL)
2533 return 0;
2534
2535 if (flags & EW_ICASE)
2536 glob_flags |= WILD_ICASE;
2537 if (flags & EW_ADDSLASH)
2538 glob_flags |= WILD_ADD_SLASH;
zeertzjq3770f4c2023-01-22 18:38:51 +00002539 globpath(paths, pattern, gap, glob_flags, FALSE);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002540 vim_free(paths);
2541
2542 return gap->ga_len;
2543}
2544
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002545
2546/*
2547 * Converts a file name into a canonical form. It simplifies a file name into
2548 * its simplest form by stripping out unneeded components, if any. The
2549 * resulting file name is simplified in place and will either be the same
2550 * length as that supplied, or shorter.
2551 */
2552 void
2553simplify_filename(char_u *filename)
2554{
2555#ifndef AMIGA // Amiga doesn't have "..", it uses "/"
2556 int components = 0;
2557 char_u *p, *tail, *start;
2558 int stripping_disabled = FALSE;
2559 int relative = TRUE;
2560
2561 p = filename;
2562# ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmanan6df0f272021-12-16 13:06:10 +00002563 if (p[0] != NUL && p[1] == ':') // skip "x:"
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002564 p += 2;
2565# endif
2566
2567 if (vim_ispathsep(*p))
2568 {
2569 relative = FALSE;
2570 do
2571 ++p;
2572 while (vim_ispathsep(*p));
2573 }
2574 start = p; // remember start after "c:/" or "/" or "///"
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02002575#ifdef UNIX
2576 // Posix says that "//path" is unchanged but "///path" is "/path".
2577 if (start > filename + 2)
2578 {
2579 STRMOVE(filename + 1, p);
2580 start = p = filename + 1;
2581 }
2582#endif
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002583
2584 do
2585 {
2586 // At this point "p" is pointing to the char following a single "/"
2587 // or "p" is at the "start" of the (absolute or relative) path name.
2588# ifdef VMS
2589 // VMS allows device:[path] - don't strip the [ in directory
2590 if ((*p == '[' || *p == '<') && p > filename && p[-1] == ':')
2591 {
2592 // :[ or :< composition: vms directory component
2593 ++components;
2594 p = getnextcomp(p + 1);
2595 }
2596 // allow remote calls as host"user passwd"::device:[path]
2597 else if (p[0] == ':' && p[1] == ':' && p > filename && p[-1] == '"' )
2598 {
2599 // ":: composition: vms host/passwd component
2600 ++components;
2601 p = getnextcomp(p + 2);
2602 }
2603 else
2604# endif
2605 if (vim_ispathsep(*p))
2606 STRMOVE(p, p + 1); // remove duplicate "/"
2607 else if (p[0] == '.' && (vim_ispathsep(p[1]) || p[1] == NUL))
2608 {
2609 if (p == start && relative)
2610 p += 1 + (p[1] != NUL); // keep single "." or leading "./"
2611 else
2612 {
2613 // Strip "./" or ".///". If we are at the end of the file name
2614 // and there is no trailing path separator, either strip "/." if
2615 // we are after "start", or strip "." if we are at the beginning
2616 // of an absolute path name .
2617 tail = p + 1;
2618 if (p[1] != NUL)
2619 while (vim_ispathsep(*tail))
2620 MB_PTR_ADV(tail);
2621 else if (p > start)
2622 --p; // strip preceding path separator
2623 STRMOVE(p, tail);
2624 }
2625 }
2626 else if (p[0] == '.' && p[1] == '.' &&
2627 (vim_ispathsep(p[2]) || p[2] == NUL))
2628 {
2629 // Skip to after ".." or "../" or "..///".
2630 tail = p + 2;
2631 while (vim_ispathsep(*tail))
2632 MB_PTR_ADV(tail);
2633
2634 if (components > 0) // strip one preceding component
2635 {
2636 int do_strip = FALSE;
2637 char_u saved_char;
2638 stat_T st;
2639
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002640 // Don't strip for an erroneous file name.
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002641 if (!stripping_disabled)
2642 {
2643 // If the preceding component does not exist in the file
2644 // system, we strip it. On Unix, we don't accept a symbolic
2645 // link that refers to a non-existent file.
2646 saved_char = p[-1];
2647 p[-1] = NUL;
2648# ifdef UNIX
2649 if (mch_lstat((char *)filename, &st) < 0)
2650# else
2651 if (mch_stat((char *)filename, &st) < 0)
2652# endif
2653 do_strip = TRUE;
2654 p[-1] = saved_char;
2655
2656 --p;
2657 // Skip back to after previous '/'.
2658 while (p > start && !after_pathsep(start, p))
2659 MB_PTR_BACK(start, p);
2660
2661 if (!do_strip)
2662 {
2663 // If the component exists in the file system, check
2664 // that stripping it won't change the meaning of the
2665 // file name. First get information about the
2666 // unstripped file name. This may fail if the component
2667 // to strip is not a searchable directory (but a regular
2668 // file, for instance), since the trailing "/.." cannot
2669 // be applied then. We don't strip it then since we
2670 // don't want to replace an erroneous file name by
2671 // a valid one, and we disable stripping of later
2672 // components.
2673 saved_char = *tail;
2674 *tail = NUL;
2675 if (mch_stat((char *)filename, &st) >= 0)
2676 do_strip = TRUE;
2677 else
2678 stripping_disabled = TRUE;
2679 *tail = saved_char;
2680# ifdef UNIX
2681 if (do_strip)
2682 {
2683 stat_T new_st;
2684
2685 // On Unix, the check for the unstripped file name
2686 // above works also for a symbolic link pointing to
2687 // a searchable directory. But then the parent of
2688 // the directory pointed to by the link must be the
2689 // same as the stripped file name. (The latter
2690 // exists in the file system since it is the
2691 // component's parent directory.)
2692 if (p == start && relative)
2693 (void)mch_stat(".", &new_st);
2694 else
2695 {
2696 saved_char = *p;
2697 *p = NUL;
2698 (void)mch_stat((char *)filename, &new_st);
2699 *p = saved_char;
2700 }
2701
2702 if (new_st.st_ino != st.st_ino ||
2703 new_st.st_dev != st.st_dev)
2704 {
2705 do_strip = FALSE;
2706 // We don't disable stripping of later
2707 // components since the unstripped path name is
2708 // still valid.
2709 }
2710 }
2711# endif
2712 }
2713 }
2714
2715 if (!do_strip)
2716 {
2717 // Skip the ".." or "../" and reset the counter for the
2718 // components that might be stripped later on.
2719 p = tail;
2720 components = 0;
2721 }
2722 else
2723 {
2724 // Strip previous component. If the result would get empty
2725 // and there is no trailing path separator, leave a single
2726 // "." instead. If we are at the end of the file name and
2727 // there is no trailing path separator and a preceding
2728 // component is left after stripping, strip its trailing
2729 // path separator as well.
2730 if (p == start && relative && tail[-1] == '.')
2731 {
2732 *p++ = '.';
2733 *p = NUL;
2734 }
2735 else
2736 {
2737 if (p > start && tail[-1] == '.')
2738 --p;
2739 STRMOVE(p, tail); // strip previous component
2740 }
2741
2742 --components;
2743 }
2744 }
2745 else if (p == start && !relative) // leading "/.." or "/../"
2746 STRMOVE(p, tail); // strip ".." or "../"
2747 else
2748 {
2749 if (p == start + 2 && p[-2] == '.') // leading "./../"
2750 {
2751 STRMOVE(p - 2, p); // strip leading "./"
2752 tail -= 2;
2753 }
2754 p = tail; // skip to char after ".." or "../"
2755 }
2756 }
2757 else
2758 {
2759 ++components; // simple path component
2760 p = getnextcomp(p);
2761 }
2762 } while (*p != NUL);
2763#endif // !AMIGA
2764}
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002765
2766#if defined(FEAT_EVAL) || defined(PROTO)
2767/*
2768 * "simplify()" function
2769 */
2770 void
2771f_simplify(typval_T *argvars, typval_T *rettv)
2772{
2773 char_u *p;
2774
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002775 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2776 return;
2777
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02002778 p = tv_get_string_strict(&argvars[0]);
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002779 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002780 simplify_filename(rettv->vval.v_string); // simplify in place
Bram Moolenaarb005cd82019-09-04 15:54:55 +02002781 rettv->v_type = VAR_STRING;
2782}
2783#endif // FEAT_EVAL