blob: 75d4a541d0fccc050a761734994f372d956fe4ac [file] [log] [blame]
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * findfile.c: Search for files in directories listed in 'path'
12 */
13
14#include "vim.h"
15
16/*
17 * File searching functions for 'path', 'tags' and 'cdpath' options.
18 * External visible functions:
19 * vim_findfile_init() creates/initialises the search context
20 * vim_findfile_free_visited() free list of visited files/dirs of search
21 * context
22 * vim_findfile() find a file in the search context
23 * vim_findfile_cleanup() cleanup/free search context created by
24 * vim_findfile_init()
25 *
26 * All static functions and variables start with 'ff_'
27 *
28 * In general it works like this:
29 * First you create yourself a search context by calling vim_findfile_init().
30 * It is possible to give a search context from a previous call to
31 * vim_findfile_init(), so it can be reused. After this you call vim_findfile()
32 * until you are satisfied with the result or it returns NULL. On every call it
33 * returns the next file which matches the conditions given to
34 * vim_findfile_init(). If it doesn't find a next file it returns NULL.
35 *
36 * It is possible to call vim_findfile_init() again to reinitialise your search
37 * with some new parameters. Don't forget to pass your old search context to
38 * it, so it can reuse it and especially reuse the list of already visited
39 * directories. If you want to delete the list of already visited directories
40 * simply call vim_findfile_free_visited().
41 *
42 * When you are done call vim_findfile_cleanup() to free the search context.
43 *
44 * The function vim_findfile_init() has a long comment, which describes the
45 * needed parameters.
46 *
47 *
48 *
49 * ATTENTION:
50 * ==========
51 * Also we use an allocated search context here, this functions are NOT
52 * thread-safe!!!!!
53 *
54 * To minimize parameter passing (or because I'm to lazy), only the
55 * external visible functions get a search context as a parameter. This is
56 * then assigned to a static global, which is used throughout the local
57 * functions.
58 */
59
60/*
61 * type for the directory search stack
62 */
63typedef struct ff_stack
64{
65 struct ff_stack *ffs_prev;
66
67 // the fix part (no wildcards) and the part containing the wildcards
68 // of the search path
69 char_u *ffs_fix_path;
70#ifdef FEAT_PATH_EXTRA
71 char_u *ffs_wc_path;
72#endif
73
74 // files/dirs found in the above directory, matched by the first wildcard
75 // of wc_part
76 char_u **ffs_filearray;
77 int ffs_filearray_size;
78 char_u ffs_filearray_cur; // needed for partly handled dirs
79
80 // to store status of partly handled directories
81 // 0: we work on this directory for the first time
82 // 1: this directory was partly searched in an earlier step
83 int ffs_stage;
84
85 // How deep are we in the directory tree?
86 // Counts backward from value of level parameter to vim_findfile_init
87 int ffs_level;
88
89 // Did we already expand '**' to an empty string?
90 int ffs_star_star_empty;
91} ff_stack_T;
92
93/*
94 * type for already visited directories or files.
95 */
96typedef struct ff_visited
97{
98 struct ff_visited *ffv_next;
99
100#ifdef FEAT_PATH_EXTRA
101 // Visited directories are different if the wildcard string are
102 // different. So we have to save it.
103 char_u *ffv_wc_path;
104#endif
105 // for unix use inode etc for comparison (needed because of links), else
106 // use filename.
107#ifdef UNIX
108 int ffv_dev_valid; // ffv_dev and ffv_ino were set
109 dev_t ffv_dev; // device number
110 ino_t ffv_ino; // inode number
111#endif
112 // The memory for this struct is allocated according to the length of
113 // ffv_fname.
114 char_u ffv_fname[1]; // actually longer
115} ff_visited_T;
116
117/*
118 * We might have to manage several visited lists during a search.
119 * This is especially needed for the tags option. If tags is set to:
120 * "./++/tags,./++/TAGS,++/tags" (replace + with *)
121 * So we have to do 3 searches:
122 * 1) search from the current files directory downward for the file "tags"
123 * 2) search from the current files directory downward for the file "TAGS"
124 * 3) search from Vims current directory downwards for the file "tags"
125 * As you can see, the first and the third search are for the same file, so for
126 * the third search we can use the visited list of the first search. For the
127 * second search we must start from a empty visited list.
128 * The struct ff_visited_list_hdr is used to manage a linked list of already
129 * visited lists.
130 */
131typedef struct ff_visited_list_hdr
132{
133 struct ff_visited_list_hdr *ffvl_next;
134
135 // the filename the attached visited list is for
136 char_u *ffvl_filename;
137
138 ff_visited_T *ffvl_visited_list;
139
140} ff_visited_list_hdr_T;
141
142
143/*
144 * '**' can be expanded to several directory levels.
145 * Set the default maximum depth.
146 */
147#define FF_MAX_STAR_STAR_EXPAND ((char_u)30)
148
149/*
150 * The search context:
151 * ffsc_stack_ptr: the stack for the dirs to search
152 * ffsc_visited_list: the currently active visited list
153 * ffsc_dir_visited_list: the currently active visited list for search dirs
154 * ffsc_visited_lists_list: the list of all visited lists
155 * ffsc_dir_visited_lists_list: the list of all visited lists for search dirs
156 * ffsc_file_to_search: the file to search for
157 * ffsc_start_dir: the starting directory, if search path was relative
158 * ffsc_fix_path: the fix part of the given path (without wildcards)
159 * Needed for upward search.
160 * ffsc_wc_path: the part of the given path containing wildcards
161 * ffsc_level: how many levels of dirs to search downwards
162 * ffsc_stopdirs_v: array of stop directories for upward search
163 * ffsc_find_what: FINDFILE_BOTH, FINDFILE_DIR or FINDFILE_FILE
164 * ffsc_tagfile: searching for tags file, don't use 'suffixesadd'
165 */
166typedef struct ff_search_ctx_T
167{
168 ff_stack_T *ffsc_stack_ptr;
169 ff_visited_list_hdr_T *ffsc_visited_list;
170 ff_visited_list_hdr_T *ffsc_dir_visited_list;
171 ff_visited_list_hdr_T *ffsc_visited_lists_list;
172 ff_visited_list_hdr_T *ffsc_dir_visited_lists_list;
173 char_u *ffsc_file_to_search;
174 char_u *ffsc_start_dir;
175 char_u *ffsc_fix_path;
176#ifdef FEAT_PATH_EXTRA
177 char_u *ffsc_wc_path;
178 int ffsc_level;
179 char_u **ffsc_stopdirs_v;
180#endif
181 int ffsc_find_what;
182 int ffsc_tagfile;
183} ff_search_ctx_T;
184
185// locally needed functions
186#ifdef FEAT_PATH_EXTRA
187static int ff_check_visited(ff_visited_T **, char_u *, char_u *);
188#else
189static int ff_check_visited(ff_visited_T **, char_u *);
190#endif
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200191static void vim_findfile_free_visited(void *search_ctx_arg);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100192static void vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp);
193static void ff_free_visited_list(ff_visited_T *vl);
194static ff_visited_list_hdr_T* ff_get_visited_list(char_u *, ff_visited_list_hdr_T **list_headp);
195
196static void ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr);
197static ff_stack_T *ff_pop(ff_search_ctx_T *search_ctx);
198static void ff_clear(ff_search_ctx_T *search_ctx);
199static void ff_free_stack_element(ff_stack_T *stack_ptr);
200#ifdef FEAT_PATH_EXTRA
201static ff_stack_T *ff_create_stack_element(char_u *, char_u *, int, int);
202#else
203static ff_stack_T *ff_create_stack_element(char_u *, int, int);
204#endif
205#ifdef FEAT_PATH_EXTRA
206static int ff_path_in_stoplist(char_u *, int, char_u **);
207#endif
208
209static char_u e_pathtoolong[] = N_("E854: path too long for completion");
210
211static char_u *ff_expand_buffer = NULL; // used for expanding filenames
212
213#if 0
214/*
215 * if someone likes findfirst/findnext, here are the functions
216 * NOT TESTED!!
217 */
218
219static void *ff_fn_search_context = NULL;
220
221 char_u *
222vim_findfirst(char_u *path, char_u *filename, int level)
223{
224 ff_fn_search_context =
225 vim_findfile_init(path, filename, NULL, level, TRUE, FALSE,
226 ff_fn_search_context, rel_fname);
227 if (NULL == ff_fn_search_context)
228 return NULL;
229 else
230 return vim_findnext()
231}
232
233 char_u *
234vim_findnext(void)
235{
236 char_u *ret = vim_findfile(ff_fn_search_context);
237
238 if (NULL == ret)
239 {
240 vim_findfile_cleanup(ff_fn_search_context);
241 ff_fn_search_context = NULL;
242 }
243 return ret;
244}
245#endif
246
247/*
248 * Initialization routine for vim_findfile().
249 *
250 * Returns the newly allocated search context or NULL if an error occurred.
251 *
252 * Don't forget to clean up by calling vim_findfile_cleanup() if you are done
253 * with the search context.
254 *
255 * Find the file 'filename' in the directory 'path'.
256 * The parameter 'path' may contain wildcards. If so only search 'level'
257 * directories deep. The parameter 'level' is the absolute maximum and is
258 * not related to restricts given to the '**' wildcard. If 'level' is 100
259 * and you use '**200' vim_findfile() will stop after 100 levels.
260 *
261 * 'filename' cannot contain wildcards! It is used as-is, no backslashes to
262 * escape special characters.
263 *
264 * If 'stopdirs' is not NULL and nothing is found downward, the search is
265 * restarted on the next higher directory level. This is repeated until the
266 * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the
267 * format ";*<dirname>*\(;<dirname>\)*;\=$".
268 *
269 * If the 'path' is relative, the starting dir for the search is either VIM's
270 * current dir or if the path starts with "./" the current files dir.
271 * If the 'path' is absolute, the starting dir is that part of the path before
272 * the first wildcard.
273 *
274 * Upward search is only done on the starting dir.
275 *
276 * If 'free_visited' is TRUE the list of already visited files/directories is
277 * cleared. Set this to FALSE if you just want to search from another
278 * directory, but want to be sure that no directory from a previous search is
279 * searched again. This is useful if you search for a file at different places.
280 * The list of visited files/dirs can also be cleared with the function
281 * vim_findfile_free_visited().
282 *
283 * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for
284 * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both.
285 *
286 * A search context returned by a previous call to vim_findfile_init() can be
287 * passed in the parameter "search_ctx_arg". This context is reused and
288 * reinitialized with the new parameters. The list of already visited
289 * directories from this context is only deleted if the parameter
290 * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed
291 * if the reinitialization fails.
292 *
293 * If you don't have a search context from a previous call "search_ctx_arg"
294 * must be NULL.
295 *
296 * This function silently ignores a few errors, vim_findfile() will have
297 * limited functionality then.
298 */
299 void *
300vim_findfile_init(
301 char_u *path,
302 char_u *filename,
303 char_u *stopdirs UNUSED,
304 int level,
305 int free_visited,
306 int find_what,
307 void *search_ctx_arg,
308 int tagfile, // expanding names of tags files
309 char_u *rel_fname) // file name to use for "."
310{
311#ifdef FEAT_PATH_EXTRA
312 char_u *wc_part;
313#endif
314 ff_stack_T *sptr;
315 ff_search_ctx_T *search_ctx;
316
317 // If a search context is given by the caller, reuse it, else allocate a
318 // new one.
319 if (search_ctx_arg != NULL)
320 search_ctx = search_ctx_arg;
321 else
322 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200323 search_ctx = ALLOC_ONE(ff_search_ctx_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100324 if (search_ctx == NULL)
325 goto error_return;
326 vim_memset(search_ctx, 0, sizeof(ff_search_ctx_T));
327 }
328 search_ctx->ffsc_find_what = find_what;
329 search_ctx->ffsc_tagfile = tagfile;
330
331 // clear the search context, but NOT the visited lists
332 ff_clear(search_ctx);
333
334 // clear visited list if wanted
335 if (free_visited == TRUE)
336 vim_findfile_free_visited(search_ctx);
337 else
338 {
339 // Reuse old visited lists. Get the visited list for the given
340 // filename. If no list for the current filename exists, creates a new
341 // one.
342 search_ctx->ffsc_visited_list = ff_get_visited_list(filename,
343 &search_ctx->ffsc_visited_lists_list);
344 if (search_ctx->ffsc_visited_list == NULL)
345 goto error_return;
346 search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename,
347 &search_ctx->ffsc_dir_visited_lists_list);
348 if (search_ctx->ffsc_dir_visited_list == NULL)
349 goto error_return;
350 }
351
352 if (ff_expand_buffer == NULL)
353 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200354 ff_expand_buffer = alloc(MAXPATHL);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100355 if (ff_expand_buffer == NULL)
356 goto error_return;
357 }
358
359 // Store information on starting dir now if path is relative.
360 // If path is absolute, we do that later.
361 if (path[0] == '.'
362 && (vim_ispathsep(path[1]) || path[1] == NUL)
363 && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL)
364 && rel_fname != NULL)
365 {
366 int len = (int)(gettail(rel_fname) - rel_fname);
367
368 if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL)
369 {
370 // Make the start dir an absolute path name.
371 vim_strncpy(ff_expand_buffer, rel_fname, len);
372 search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE);
373 }
374 else
375 search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len);
376 if (search_ctx->ffsc_start_dir == NULL)
377 goto error_return;
378 if (*++path != NUL)
379 ++path;
380 }
381 else if (*path == NUL || !vim_isAbsName(path))
382 {
383#ifdef BACKSLASH_IN_FILENAME
384 // "c:dir" needs "c:" to be expanded, otherwise use current dir
385 if (*path != NUL && path[1] == ':')
386 {
387 char_u drive[3];
388
389 drive[0] = path[0];
390 drive[1] = ':';
391 drive[2] = NUL;
392 if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
393 goto error_return;
394 path += 2;
395 }
396 else
397#endif
398 if (mch_dirname(ff_expand_buffer, MAXPATHL) == FAIL)
399 goto error_return;
400
401 search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer);
402 if (search_ctx->ffsc_start_dir == NULL)
403 goto error_return;
404
405#ifdef BACKSLASH_IN_FILENAME
406 // A path that starts with "/dir" is relative to the drive, not to the
407 // directory (but not for "//machine/dir"). Only use the drive name.
408 if ((*path == '/' || *path == '\\')
409 && path[1] != path[0]
410 && search_ctx->ffsc_start_dir[1] == ':')
411 search_ctx->ffsc_start_dir[2] = NUL;
412#endif
413 }
414
415#ifdef FEAT_PATH_EXTRA
416 /*
417 * If stopdirs are given, split them into an array of pointers.
418 * If this fails (mem allocation), there is no upward search at all or a
419 * stop directory is not recognized -> continue silently.
420 * If stopdirs just contains a ";" or is empty,
421 * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This
422 * is handled as unlimited upward search. See function
423 * ff_path_in_stoplist() for details.
424 */
425 if (stopdirs != NULL)
426 {
427 char_u *walker = stopdirs;
428 int dircount;
429
430 while (*walker == ';')
431 walker++;
432
433 dircount = 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200434 search_ctx->ffsc_stopdirs_v = ALLOC_ONE(char_u *);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100435
436 if (search_ctx->ffsc_stopdirs_v != NULL)
437 {
438 do
439 {
440 char_u *helper;
441 void *ptr;
442
443 helper = walker;
444 ptr = vim_realloc(search_ctx->ffsc_stopdirs_v,
445 (dircount + 1) * sizeof(char_u *));
446 if (ptr)
447 search_ctx->ffsc_stopdirs_v = ptr;
448 else
449 // ignore, keep what we have and continue
450 break;
451 walker = vim_strchr(walker, ';');
452 if (walker)
453 {
454 search_ctx->ffsc_stopdirs_v[dircount-1] =
455 vim_strnsave(helper, (int)(walker - helper));
456 walker++;
457 }
458 else
459 // this might be "", which means ascent till top
460 // of directory tree.
461 search_ctx->ffsc_stopdirs_v[dircount-1] =
462 vim_strsave(helper);
463
464 dircount++;
465
466 } while (walker != NULL);
467 search_ctx->ffsc_stopdirs_v[dircount-1] = NULL;
468 }
469 }
470#endif
471
472#ifdef FEAT_PATH_EXTRA
473 search_ctx->ffsc_level = level;
474
475 /*
476 * split into:
477 * -fix path
478 * -wildcard_stuff (might be NULL)
479 */
480 wc_part = vim_strchr(path, '*');
481 if (wc_part != NULL)
482 {
483 int llevel;
484 int len;
485 char *errpt;
486
487 // save the fix part of the path
488 search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path));
489
490 /*
491 * copy wc_path and add restricts to the '**' wildcard.
492 * The octet after a '**' is used as a (binary) counter.
493 * So '**3' is transposed to '**^C' ('^C' is ASCII value 3)
494 * or '**76' is transposed to '**N'( 'N' is ASCII value 76).
495 * For EBCDIC you get different character values.
496 * If no restrict is given after '**' the default is used.
497 * Due to this technique the path looks awful if you print it as a
498 * string.
499 */
500 len = 0;
501 while (*wc_part != NUL)
502 {
503 if (len + 5 >= MAXPATHL)
504 {
505 emsg(_(e_pathtoolong));
506 break;
507 }
508 if (STRNCMP(wc_part, "**", 2) == 0)
509 {
510 ff_expand_buffer[len++] = *wc_part++;
511 ff_expand_buffer[len++] = *wc_part++;
512
513 llevel = strtol((char *)wc_part, &errpt, 10);
514 if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255)
515 ff_expand_buffer[len++] = llevel;
516 else if ((char_u *)errpt != wc_part && llevel == 0)
517 // restrict is 0 -> remove already added '**'
518 len -= 2;
519 else
520 ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND;
521 wc_part = (char_u *)errpt;
522 if (*wc_part != NUL && !vim_ispathsep(*wc_part))
523 {
524 semsg(_("E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR);
525 goto error_return;
526 }
527 }
528 else
529 ff_expand_buffer[len++] = *wc_part++;
530 }
531 ff_expand_buffer[len] = NUL;
532 search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer);
533
534 if (search_ctx->ffsc_wc_path == NULL)
535 goto error_return;
536 }
537 else
538#endif
539 search_ctx->ffsc_fix_path = vim_strsave(path);
540
541 if (search_ctx->ffsc_start_dir == NULL)
542 {
543 // store the fix part as startdir.
544 // This is needed if the parameter path is fully qualified.
545 search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path);
546 if (search_ctx->ffsc_start_dir == NULL)
547 goto error_return;
548 search_ctx->ffsc_fix_path[0] = NUL;
549 }
550
551 // create an absolute path
552 if (STRLEN(search_ctx->ffsc_start_dir)
553 + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL)
554 {
555 emsg(_(e_pathtoolong));
556 goto error_return;
557 }
558 STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir);
559 add_pathsep(ff_expand_buffer);
560 {
561 int eb_len = (int)STRLEN(ff_expand_buffer);
562 char_u *buf = alloc(eb_len
563 + (int)STRLEN(search_ctx->ffsc_fix_path) + 1);
564
565 STRCPY(buf, ff_expand_buffer);
566 STRCPY(buf + eb_len, search_ctx->ffsc_fix_path);
567 if (mch_isdir(buf))
568 {
569 STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path);
570 add_pathsep(ff_expand_buffer);
571 }
572#ifdef FEAT_PATH_EXTRA
573 else
574 {
575 char_u *p = gettail(search_ctx->ffsc_fix_path);
576 char_u *wc_path = NULL;
577 char_u *temp = NULL;
578 int len = 0;
579
580 if (p > search_ctx->ffsc_fix_path)
581 {
582 len = (int)(p - search_ctx->ffsc_fix_path) - 1;
583 STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len);
584 add_pathsep(ff_expand_buffer);
585 }
586 else
587 len = (int)STRLEN(search_ctx->ffsc_fix_path);
588
589 if (search_ctx->ffsc_wc_path != NULL)
590 {
591 wc_path = vim_strsave(search_ctx->ffsc_wc_path);
Bram Moolenaar51e14382019-05-25 20:21:28 +0200592 temp = alloc(STRLEN(search_ctx->ffsc_wc_path)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100593 + STRLEN(search_ctx->ffsc_fix_path + len)
Bram Moolenaar51e14382019-05-25 20:21:28 +0200594 + 1);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100595 if (temp == NULL || wc_path == NULL)
596 {
597 vim_free(buf);
598 vim_free(temp);
599 vim_free(wc_path);
600 goto error_return;
601 }
602
603 STRCPY(temp, search_ctx->ffsc_fix_path + len);
604 STRCAT(temp, search_ctx->ffsc_wc_path);
605 vim_free(search_ctx->ffsc_wc_path);
606 vim_free(wc_path);
607 search_ctx->ffsc_wc_path = temp;
608 }
609 }
610#endif
611 vim_free(buf);
612 }
613
614 sptr = ff_create_stack_element(ff_expand_buffer,
615#ifdef FEAT_PATH_EXTRA
616 search_ctx->ffsc_wc_path,
617#endif
618 level, 0);
619
620 if (sptr == NULL)
621 goto error_return;
622
623 ff_push(search_ctx, sptr);
624
625 search_ctx->ffsc_file_to_search = vim_strsave(filename);
626 if (search_ctx->ffsc_file_to_search == NULL)
627 goto error_return;
628
629 return search_ctx;
630
631error_return:
632 /*
633 * We clear the search context now!
634 * Even when the caller gave us a (perhaps valid) context we free it here,
635 * as we might have already destroyed it.
636 */
637 vim_findfile_cleanup(search_ctx);
638 return NULL;
639}
640
641#if defined(FEAT_PATH_EXTRA) || defined(PROTO)
642/*
643 * Get the stopdir string. Check that ';' is not escaped.
644 */
645 char_u *
646vim_findfile_stopdir(char_u *buf)
647{
648 char_u *r_ptr = buf;
649
650 while (*r_ptr != NUL && *r_ptr != ';')
651 {
652 if (r_ptr[0] == '\\' && r_ptr[1] == ';')
653 {
654 // Overwrite the escape char,
655 // use STRLEN(r_ptr) to move the trailing '\0'.
656 STRMOVE(r_ptr, r_ptr + 1);
657 r_ptr++;
658 }
659 r_ptr++;
660 }
661 if (*r_ptr == ';')
662 {
663 *r_ptr = 0;
664 r_ptr++;
665 }
666 else if (*r_ptr == NUL)
667 r_ptr = NULL;
668 return r_ptr;
669}
670#endif
671
672/*
673 * Clean up the given search context. Can handle a NULL pointer.
674 */
675 void
676vim_findfile_cleanup(void *ctx)
677{
678 if (ctx == NULL)
679 return;
680
681 vim_findfile_free_visited(ctx);
682 ff_clear(ctx);
683 vim_free(ctx);
684}
685
686/*
687 * Find a file in a search context.
688 * The search context was created with vim_findfile_init() above.
689 * Return a pointer to an allocated file name or NULL if nothing found.
690 * To get all matching files call this function until you get NULL.
691 *
692 * If the passed search_context is NULL, NULL is returned.
693 *
694 * The search algorithm is depth first. To change this replace the
695 * stack with a list (don't forget to leave partly searched directories on the
696 * top of the list).
697 */
698 char_u *
699vim_findfile(void *search_ctx_arg)
700{
701 char_u *file_path;
702#ifdef FEAT_PATH_EXTRA
703 char_u *rest_of_wildcards;
704 char_u *path_end = NULL;
705#endif
706 ff_stack_T *stackp;
707#if defined(FEAT_SEARCHPATH) || defined(FEAT_PATH_EXTRA)
708 int len;
709#endif
710 int i;
711 char_u *p;
712#ifdef FEAT_SEARCHPATH
713 char_u *suf;
714#endif
715 ff_search_ctx_T *search_ctx;
716
717 if (search_ctx_arg == NULL)
718 return NULL;
719
720 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
721
722 /*
723 * filepath is used as buffer for various actions and as the storage to
724 * return a found filename.
725 */
Bram Moolenaar51e14382019-05-25 20:21:28 +0200726 if ((file_path = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100727 return NULL;
728
729#ifdef FEAT_PATH_EXTRA
730 // store the end of the start dir -- needed for upward search
731 if (search_ctx->ffsc_start_dir != NULL)
732 path_end = &search_ctx->ffsc_start_dir[
733 STRLEN(search_ctx->ffsc_start_dir)];
734#endif
735
736#ifdef FEAT_PATH_EXTRA
737 // upward search loop
738 for (;;)
739 {
740#endif
741 // downward search loop
742 for (;;)
743 {
744 // check if user user wants to stop the search
745 ui_breakcheck();
746 if (got_int)
747 break;
748
749 // get directory to work on from stack
750 stackp = ff_pop(search_ctx);
751 if (stackp == NULL)
752 break;
753
754 /*
755 * TODO: decide if we leave this test in
756 *
757 * GOOD: don't search a directory(-tree) twice.
758 * BAD: - check linked list for every new directory entered.
759 * - check for double files also done below
760 *
761 * Here we check if we already searched this directory.
762 * We already searched a directory if:
763 * 1) The directory is the same.
764 * 2) We would use the same wildcard string.
765 *
766 * Good if you have links on same directory via several ways
767 * or you have selfreferences in directories (e.g. SuSE Linux 6.3:
768 * /etc/rc.d/init.d is linked to /etc/rc.d -> endless loop)
769 *
770 * This check is only needed for directories we work on for the
771 * first time (hence stackp->ff_filearray == NULL)
772 */
773 if (stackp->ffs_filearray == NULL
774 && ff_check_visited(&search_ctx->ffsc_dir_visited_list
775 ->ffvl_visited_list,
776 stackp->ffs_fix_path
777#ifdef FEAT_PATH_EXTRA
778 , stackp->ffs_wc_path
779#endif
780 ) == FAIL)
781 {
782#ifdef FF_VERBOSE
783 if (p_verbose >= 5)
784 {
785 verbose_enter_scroll();
786 smsg("Already Searched: %s (%s)",
787 stackp->ffs_fix_path, stackp->ffs_wc_path);
788 // don't overwrite this either
789 msg_puts("\n");
790 verbose_leave_scroll();
791 }
792#endif
793 ff_free_stack_element(stackp);
794 continue;
795 }
796#ifdef FF_VERBOSE
797 else if (p_verbose >= 5)
798 {
799 verbose_enter_scroll();
800 smsg("Searching: %s (%s)",
801 stackp->ffs_fix_path, stackp->ffs_wc_path);
802 // don't overwrite this either
803 msg_puts("\n");
804 verbose_leave_scroll();
805 }
806#endif
807
808 // check depth
809 if (stackp->ffs_level <= 0)
810 {
811 ff_free_stack_element(stackp);
812 continue;
813 }
814
815 file_path[0] = NUL;
816
817 /*
818 * If no filearray till now expand wildcards
819 * The function expand_wildcards() can handle an array of paths
820 * and all possible expands are returned in one array. We use this
821 * to handle the expansion of '**' into an empty string.
822 */
823 if (stackp->ffs_filearray == NULL)
824 {
825 char_u *dirptrs[2];
826
827 // we use filepath to build the path expand_wildcards() should
828 // expand.
829 dirptrs[0] = file_path;
830 dirptrs[1] = NULL;
831
832 // if we have a start dir copy it in
833 if (!vim_isAbsName(stackp->ffs_fix_path)
834 && search_ctx->ffsc_start_dir)
835 {
836 if (STRLEN(search_ctx->ffsc_start_dir) + 1 < MAXPATHL)
837 {
838 STRCPY(file_path, search_ctx->ffsc_start_dir);
839 add_pathsep(file_path);
840 }
841 else
842 {
843 ff_free_stack_element(stackp);
844 goto fail;
845 }
846 }
847
848 // append the fix part of the search path
849 if (STRLEN(file_path) + STRLEN(stackp->ffs_fix_path) + 1
850 < MAXPATHL)
851 {
852 STRCAT(file_path, stackp->ffs_fix_path);
853 add_pathsep(file_path);
854 }
855 else
856 {
857 ff_free_stack_element(stackp);
858 goto fail;
859 }
860
861#ifdef FEAT_PATH_EXTRA
862 rest_of_wildcards = stackp->ffs_wc_path;
863 if (*rest_of_wildcards != NUL)
864 {
865 len = (int)STRLEN(file_path);
866 if (STRNCMP(rest_of_wildcards, "**", 2) == 0)
867 {
868 // pointer to the restrict byte
869 // The restrict byte is not a character!
870 p = rest_of_wildcards + 2;
871
872 if (*p > 0)
873 {
874 (*p)--;
875 if (len + 1 < MAXPATHL)
876 file_path[len++] = '*';
877 else
878 {
879 ff_free_stack_element(stackp);
880 goto fail;
881 }
882 }
883
884 if (*p == 0)
885 {
886 // remove '**<numb> from wildcards
887 STRMOVE(rest_of_wildcards, rest_of_wildcards + 3);
888 }
889 else
890 rest_of_wildcards += 3;
891
892 if (stackp->ffs_star_star_empty == 0)
893 {
894 // if not done before, expand '**' to empty
895 stackp->ffs_star_star_empty = 1;
896 dirptrs[1] = stackp->ffs_fix_path;
897 }
898 }
899
900 /*
901 * Here we copy until the next path separator or the end of
902 * the path. If we stop at a path separator, there is
903 * still something else left. This is handled below by
904 * pushing every directory returned from expand_wildcards()
905 * on the stack again for further search.
906 */
907 while (*rest_of_wildcards
908 && !vim_ispathsep(*rest_of_wildcards))
909 if (len + 1 < MAXPATHL)
910 file_path[len++] = *rest_of_wildcards++;
911 else
912 {
913 ff_free_stack_element(stackp);
914 goto fail;
915 }
916
917 file_path[len] = NUL;
918 if (vim_ispathsep(*rest_of_wildcards))
919 rest_of_wildcards++;
920 }
921#endif
922
923 /*
924 * Expand wildcards like "*" and "$VAR".
925 * If the path is a URL don't try this.
926 */
927 if (path_with_url(dirptrs[0]))
928 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200929 stackp->ffs_filearray = ALLOC_ONE(char_u *);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +0100930 if (stackp->ffs_filearray != NULL
931 && (stackp->ffs_filearray[0]
932 = vim_strsave(dirptrs[0])) != NULL)
933 stackp->ffs_filearray_size = 1;
934 else
935 stackp->ffs_filearray_size = 0;
936 }
937 else
938 // Add EW_NOTWILD because the expanded path may contain
939 // wildcard characters that are to be taken literally.
940 // This is a bit of a hack.
941 expand_wildcards((dirptrs[1] == NULL) ? 1 : 2, dirptrs,
942 &stackp->ffs_filearray_size,
943 &stackp->ffs_filearray,
944 EW_DIR|EW_ADDSLASH|EW_SILENT|EW_NOTWILD);
945
946 stackp->ffs_filearray_cur = 0;
947 stackp->ffs_stage = 0;
948 }
949#ifdef FEAT_PATH_EXTRA
950 else
951 rest_of_wildcards = &stackp->ffs_wc_path[
952 STRLEN(stackp->ffs_wc_path)];
953#endif
954
955 if (stackp->ffs_stage == 0)
956 {
957 // this is the first time we work on this directory
958#ifdef FEAT_PATH_EXTRA
959 if (*rest_of_wildcards == NUL)
960#endif
961 {
962 /*
963 * We don't have further wildcards to expand, so we have to
964 * check for the final file now.
965 */
966 for (i = stackp->ffs_filearray_cur;
967 i < stackp->ffs_filearray_size; ++i)
968 {
969 if (!path_with_url(stackp->ffs_filearray[i])
970 && !mch_isdir(stackp->ffs_filearray[i]))
971 continue; /* not a directory */
972
973 // prepare the filename to be checked for existence
974 // below
975 if (STRLEN(stackp->ffs_filearray[i]) + 1
976 + STRLEN(search_ctx->ffsc_file_to_search)
977 < MAXPATHL)
978 {
979 STRCPY(file_path, stackp->ffs_filearray[i]);
980 add_pathsep(file_path);
981 STRCAT(file_path, search_ctx->ffsc_file_to_search);
982 }
983 else
984 {
985 ff_free_stack_element(stackp);
986 goto fail;
987 }
988
989 /*
990 * Try without extra suffix and then with suffixes
991 * from 'suffixesadd'.
992 */
993#ifdef FEAT_SEARCHPATH
994 len = (int)STRLEN(file_path);
995 if (search_ctx->ffsc_tagfile)
996 suf = (char_u *)"";
997 else
998 suf = curbuf->b_p_sua;
999 for (;;)
1000#endif
1001 {
1002 // if file exists and we didn't already find it
1003 if ((path_with_url(file_path)
1004 || (mch_getperm(file_path) >= 0
1005 && (search_ctx->ffsc_find_what
1006 == FINDFILE_BOTH
1007 || ((search_ctx->ffsc_find_what
1008 == FINDFILE_DIR)
1009 == mch_isdir(file_path)))))
1010#ifndef FF_VERBOSE
1011 && (ff_check_visited(
1012 &search_ctx->ffsc_visited_list->ffvl_visited_list,
1013 file_path
1014#ifdef FEAT_PATH_EXTRA
1015 , (char_u *)""
1016#endif
1017 ) == OK)
1018#endif
1019 )
1020 {
1021#ifdef FF_VERBOSE
1022 if (ff_check_visited(
1023 &search_ctx->ffsc_visited_list->ffvl_visited_list,
1024 file_path
1025#ifdef FEAT_PATH_EXTRA
1026 , (char_u *)""
1027#endif
1028 ) == FAIL)
1029 {
1030 if (p_verbose >= 5)
1031 {
1032 verbose_enter_scroll();
1033 smsg("Already: %s",
1034 file_path);
1035 // don't overwrite this either
1036 msg_puts("\n");
1037 verbose_leave_scroll();
1038 }
1039 continue;
1040 }
1041#endif
1042
1043 // push dir to examine rest of subdirs later
1044 stackp->ffs_filearray_cur = i + 1;
1045 ff_push(search_ctx, stackp);
1046
1047 if (!path_with_url(file_path))
1048 simplify_filename(file_path);
1049 if (mch_dirname(ff_expand_buffer, MAXPATHL)
1050 == OK)
1051 {
1052 p = shorten_fname(file_path,
1053 ff_expand_buffer);
1054 if (p != NULL)
1055 STRMOVE(file_path, p);
1056 }
1057#ifdef FF_VERBOSE
1058 if (p_verbose >= 5)
1059 {
1060 verbose_enter_scroll();
1061 smsg("HIT: %s", file_path);
1062 // don't overwrite this either
1063 msg_puts("\n");
1064 verbose_leave_scroll();
1065 }
1066#endif
1067 return file_path;
1068 }
1069
1070#ifdef FEAT_SEARCHPATH
1071 // Not found or found already, try next suffix.
1072 if (*suf == NUL)
1073 break;
1074 copy_option_part(&suf, file_path + len,
1075 MAXPATHL - len, ",");
1076#endif
1077 }
1078 }
1079 }
1080#ifdef FEAT_PATH_EXTRA
1081 else
1082 {
1083 /*
1084 * still wildcards left, push the directories for further
1085 * search
1086 */
1087 for (i = stackp->ffs_filearray_cur;
1088 i < stackp->ffs_filearray_size; ++i)
1089 {
1090 if (!mch_isdir(stackp->ffs_filearray[i]))
1091 continue; // not a directory
1092
1093 ff_push(search_ctx,
1094 ff_create_stack_element(
1095 stackp->ffs_filearray[i],
1096 rest_of_wildcards,
1097 stackp->ffs_level - 1, 0));
1098 }
1099 }
1100#endif
1101 stackp->ffs_filearray_cur = 0;
1102 stackp->ffs_stage = 1;
1103 }
1104
1105#ifdef FEAT_PATH_EXTRA
1106 /*
1107 * if wildcards contains '**' we have to descent till we reach the
1108 * leaves of the directory tree.
1109 */
1110 if (STRNCMP(stackp->ffs_wc_path, "**", 2) == 0)
1111 {
1112 for (i = stackp->ffs_filearray_cur;
1113 i < stackp->ffs_filearray_size; ++i)
1114 {
1115 if (fnamecmp(stackp->ffs_filearray[i],
1116 stackp->ffs_fix_path) == 0)
1117 continue; // don't repush same directory
1118 if (!mch_isdir(stackp->ffs_filearray[i]))
1119 continue; // not a directory
1120 ff_push(search_ctx,
1121 ff_create_stack_element(stackp->ffs_filearray[i],
1122 stackp->ffs_wc_path, stackp->ffs_level - 1, 1));
1123 }
1124 }
1125#endif
1126
1127 // we are done with the current directory
1128 ff_free_stack_element(stackp);
1129
1130 }
1131
1132#ifdef FEAT_PATH_EXTRA
1133 // If we reached this, we didn't find anything downwards.
1134 // Let's check if we should do an upward search.
1135 if (search_ctx->ffsc_start_dir
1136 && search_ctx->ffsc_stopdirs_v != NULL && !got_int)
1137 {
1138 ff_stack_T *sptr;
1139
1140 // is the last starting directory in the stop list?
1141 if (ff_path_in_stoplist(search_ctx->ffsc_start_dir,
1142 (int)(path_end - search_ctx->ffsc_start_dir),
1143 search_ctx->ffsc_stopdirs_v) == TRUE)
1144 break;
1145
1146 // cut of last dir
1147 while (path_end > search_ctx->ffsc_start_dir
1148 && vim_ispathsep(*path_end))
1149 path_end--;
1150 while (path_end > search_ctx->ffsc_start_dir
1151 && !vim_ispathsep(path_end[-1]))
1152 path_end--;
1153 *path_end = 0;
1154 path_end--;
1155
1156 if (*search_ctx->ffsc_start_dir == 0)
1157 break;
1158
1159 if (STRLEN(search_ctx->ffsc_start_dir) + 1
1160 + STRLEN(search_ctx->ffsc_fix_path) < MAXPATHL)
1161 {
1162 STRCPY(file_path, search_ctx->ffsc_start_dir);
1163 add_pathsep(file_path);
1164 STRCAT(file_path, search_ctx->ffsc_fix_path);
1165 }
1166 else
1167 goto fail;
1168
1169 // create a new stack entry
1170 sptr = ff_create_stack_element(file_path,
1171 search_ctx->ffsc_wc_path, search_ctx->ffsc_level, 0);
1172 if (sptr == NULL)
1173 break;
1174 ff_push(search_ctx, sptr);
1175 }
1176 else
1177 break;
1178 }
1179#endif
1180
1181fail:
1182 vim_free(file_path);
1183 return NULL;
1184}
1185
1186/*
1187 * Free the list of lists of visited files and directories
1188 * Can handle it if the passed search_context is NULL;
1189 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001190 static void
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001191vim_findfile_free_visited(void *search_ctx_arg)
1192{
1193 ff_search_ctx_T *search_ctx;
1194
1195 if (search_ctx_arg == NULL)
1196 return;
1197
1198 search_ctx = (ff_search_ctx_T *)search_ctx_arg;
1199 vim_findfile_free_visited_list(&search_ctx->ffsc_visited_lists_list);
1200 vim_findfile_free_visited_list(&search_ctx->ffsc_dir_visited_lists_list);
1201}
1202
1203 static void
1204vim_findfile_free_visited_list(ff_visited_list_hdr_T **list_headp)
1205{
1206 ff_visited_list_hdr_T *vp;
1207
1208 while (*list_headp != NULL)
1209 {
1210 vp = (*list_headp)->ffvl_next;
1211 ff_free_visited_list((*list_headp)->ffvl_visited_list);
1212
1213 vim_free((*list_headp)->ffvl_filename);
1214 vim_free(*list_headp);
1215 *list_headp = vp;
1216 }
1217 *list_headp = NULL;
1218}
1219
1220 static void
1221ff_free_visited_list(ff_visited_T *vl)
1222{
1223 ff_visited_T *vp;
1224
1225 while (vl != NULL)
1226 {
1227 vp = vl->ffv_next;
1228#ifdef FEAT_PATH_EXTRA
1229 vim_free(vl->ffv_wc_path);
1230#endif
1231 vim_free(vl);
1232 vl = vp;
1233 }
1234 vl = NULL;
1235}
1236
1237/*
1238 * Returns the already visited list for the given filename. If none is found it
1239 * allocates a new one.
1240 */
1241 static ff_visited_list_hdr_T*
1242ff_get_visited_list(
1243 char_u *filename,
1244 ff_visited_list_hdr_T **list_headp)
1245{
1246 ff_visited_list_hdr_T *retptr = NULL;
1247
1248 // check if a visited list for the given filename exists
1249 if (*list_headp != NULL)
1250 {
1251 retptr = *list_headp;
1252 while (retptr != NULL)
1253 {
1254 if (fnamecmp(filename, retptr->ffvl_filename) == 0)
1255 {
1256#ifdef FF_VERBOSE
1257 if (p_verbose >= 5)
1258 {
1259 verbose_enter_scroll();
1260 smsg("ff_get_visited_list: FOUND list for %s",
1261 filename);
1262 // don't overwrite this either
1263 msg_puts("\n");
1264 verbose_leave_scroll();
1265 }
1266#endif
1267 return retptr;
1268 }
1269 retptr = retptr->ffvl_next;
1270 }
1271 }
1272
1273#ifdef FF_VERBOSE
1274 if (p_verbose >= 5)
1275 {
1276 verbose_enter_scroll();
1277 smsg("ff_get_visited_list: new list for %s", filename);
1278 // don't overwrite this either
1279 msg_puts("\n");
1280 verbose_leave_scroll();
1281 }
1282#endif
1283
1284 /*
1285 * if we reach this we didn't find a list and we have to allocate new list
1286 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001287 retptr = ALLOC_ONE(ff_visited_list_hdr_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001288 if (retptr == NULL)
1289 return NULL;
1290
1291 retptr->ffvl_visited_list = NULL;
1292 retptr->ffvl_filename = vim_strsave(filename);
1293 if (retptr->ffvl_filename == NULL)
1294 {
1295 vim_free(retptr);
1296 return NULL;
1297 }
1298 retptr->ffvl_next = *list_headp;
1299 *list_headp = retptr;
1300
1301 return retptr;
1302}
1303
1304#ifdef FEAT_PATH_EXTRA
1305/*
1306 * check if two wildcard paths are equal. Returns TRUE or FALSE.
1307 * They are equal if:
1308 * - both paths are NULL
1309 * - they have the same length
1310 * - char by char comparison is OK
1311 * - the only differences are in the counters behind a '**', so
1312 * '**\20' is equal to '**\24'
1313 */
1314 static int
1315ff_wc_equal(char_u *s1, char_u *s2)
1316{
1317 int i, j;
1318 int c1 = NUL;
1319 int c2 = NUL;
1320 int prev1 = NUL;
1321 int prev2 = NUL;
1322
1323 if (s1 == s2)
1324 return TRUE;
1325
1326 if (s1 == NULL || s2 == NULL)
1327 return FALSE;
1328
1329 for (i = 0, j = 0; s1[i] != NUL && s2[j] != NUL;)
1330 {
1331 c1 = PTR2CHAR(s1 + i);
1332 c2 = PTR2CHAR(s2 + j);
1333
1334 if ((p_fic ? MB_TOLOWER(c1) != MB_TOLOWER(c2) : c1 != c2)
1335 && (prev1 != '*' || prev2 != '*'))
1336 return FALSE;
1337 prev2 = prev1;
1338 prev1 = c1;
1339
1340 i += MB_PTR2LEN(s1 + i);
1341 j += MB_PTR2LEN(s2 + j);
1342 }
1343 return s1[i] == s2[j];
1344}
1345#endif
1346
1347/*
1348 * maintains the list of already visited files and dirs
1349 * returns FAIL if the given file/dir is already in the list
1350 * returns OK if it is newly added
1351 *
1352 * TODO: What to do on memory allocation problems?
1353 * -> return TRUE - Better the file is found several times instead of
1354 * never.
1355 */
1356 static int
1357ff_check_visited(
1358 ff_visited_T **visited_list,
1359 char_u *fname
1360#ifdef FEAT_PATH_EXTRA
1361 , char_u *wc_path
1362#endif
1363 )
1364{
1365 ff_visited_T *vp;
1366#ifdef UNIX
1367 stat_T st;
1368 int url = FALSE;
1369#endif
1370
1371 // For an URL we only compare the name, otherwise we compare the
1372 // device/inode (unix) or the full path name (not Unix).
1373 if (path_with_url(fname))
1374 {
1375 vim_strncpy(ff_expand_buffer, fname, MAXPATHL - 1);
1376#ifdef UNIX
1377 url = TRUE;
1378#endif
1379 }
1380 else
1381 {
1382 ff_expand_buffer[0] = NUL;
1383#ifdef UNIX
1384 if (mch_stat((char *)fname, &st) < 0)
1385#else
1386 if (vim_FullName(fname, ff_expand_buffer, MAXPATHL, TRUE) == FAIL)
1387#endif
1388 return FAIL;
1389 }
1390
1391 // check against list of already visited files
1392 for (vp = *visited_list; vp != NULL; vp = vp->ffv_next)
1393 {
1394 if (
1395#ifdef UNIX
1396 !url ? (vp->ffv_dev_valid && vp->ffv_dev == st.st_dev
1397 && vp->ffv_ino == st.st_ino)
1398 :
1399#endif
1400 fnamecmp(vp->ffv_fname, ff_expand_buffer) == 0
1401 )
1402 {
1403#ifdef FEAT_PATH_EXTRA
1404 // are the wildcard parts equal
1405 if (ff_wc_equal(vp->ffv_wc_path, wc_path) == TRUE)
1406#endif
1407 // already visited
1408 return FAIL;
1409 }
1410 }
1411
1412 /*
1413 * New file/dir. Add it to the list of visited files/dirs.
1414 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001415 vp = alloc(sizeof(ff_visited_T) + STRLEN(ff_expand_buffer));
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001416
1417 if (vp != NULL)
1418 {
1419#ifdef UNIX
1420 if (!url)
1421 {
1422 vp->ffv_dev_valid = TRUE;
1423 vp->ffv_ino = st.st_ino;
1424 vp->ffv_dev = st.st_dev;
1425 vp->ffv_fname[0] = NUL;
1426 }
1427 else
1428 {
1429 vp->ffv_dev_valid = FALSE;
1430#endif
1431 STRCPY(vp->ffv_fname, ff_expand_buffer);
1432#ifdef UNIX
1433 }
1434#endif
1435#ifdef FEAT_PATH_EXTRA
1436 if (wc_path != NULL)
1437 vp->ffv_wc_path = vim_strsave(wc_path);
1438 else
1439 vp->ffv_wc_path = NULL;
1440#endif
1441
1442 vp->ffv_next = *visited_list;
1443 *visited_list = vp;
1444 }
1445
1446 return OK;
1447}
1448
1449/*
1450 * create stack element from given path pieces
1451 */
1452 static ff_stack_T *
1453ff_create_stack_element(
1454 char_u *fix_part,
1455#ifdef FEAT_PATH_EXTRA
1456 char_u *wc_part,
1457#endif
1458 int level,
1459 int star_star_empty)
1460{
1461 ff_stack_T *new;
1462
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001463 new = ALLOC_ONE(ff_stack_T);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001464 if (new == NULL)
1465 return NULL;
1466
1467 new->ffs_prev = NULL;
1468 new->ffs_filearray = NULL;
1469 new->ffs_filearray_size = 0;
1470 new->ffs_filearray_cur = 0;
1471 new->ffs_stage = 0;
1472 new->ffs_level = level;
1473 new->ffs_star_star_empty = star_star_empty;
1474
1475 // the following saves NULL pointer checks in vim_findfile
1476 if (fix_part == NULL)
1477 fix_part = (char_u *)"";
1478 new->ffs_fix_path = vim_strsave(fix_part);
1479
1480#ifdef FEAT_PATH_EXTRA
1481 if (wc_part == NULL)
1482 wc_part = (char_u *)"";
1483 new->ffs_wc_path = vim_strsave(wc_part);
1484#endif
1485
1486 if (new->ffs_fix_path == NULL
1487#ifdef FEAT_PATH_EXTRA
1488 || new->ffs_wc_path == NULL
1489#endif
1490 )
1491 {
1492 ff_free_stack_element(new);
1493 new = NULL;
1494 }
1495
1496 return new;
1497}
1498
1499/*
1500 * Push a dir on the directory stack.
1501 */
1502 static void
1503ff_push(ff_search_ctx_T *search_ctx, ff_stack_T *stack_ptr)
1504{
1505 // check for NULL pointer, not to return an error to the user, but
1506 // to prevent a crash
1507 if (stack_ptr != NULL)
1508 {
1509 stack_ptr->ffs_prev = search_ctx->ffsc_stack_ptr;
1510 search_ctx->ffsc_stack_ptr = stack_ptr;
1511 }
1512}
1513
1514/*
1515 * Pop a dir from the directory stack.
1516 * Returns NULL if stack is empty.
1517 */
1518 static ff_stack_T *
1519ff_pop(ff_search_ctx_T *search_ctx)
1520{
1521 ff_stack_T *sptr;
1522
1523 sptr = search_ctx->ffsc_stack_ptr;
1524 if (search_ctx->ffsc_stack_ptr != NULL)
1525 search_ctx->ffsc_stack_ptr = search_ctx->ffsc_stack_ptr->ffs_prev;
1526
1527 return sptr;
1528}
1529
1530/*
1531 * free the given stack element
1532 */
1533 static void
1534ff_free_stack_element(ff_stack_T *stack_ptr)
1535{
1536 // vim_free handles possible NULL pointers
1537 vim_free(stack_ptr->ffs_fix_path);
1538#ifdef FEAT_PATH_EXTRA
1539 vim_free(stack_ptr->ffs_wc_path);
1540#endif
1541
1542 if (stack_ptr->ffs_filearray != NULL)
1543 FreeWild(stack_ptr->ffs_filearray_size, stack_ptr->ffs_filearray);
1544
1545 vim_free(stack_ptr);
1546}
1547
1548/*
1549 * Clear the search context, but NOT the visited list.
1550 */
1551 static void
1552ff_clear(ff_search_ctx_T *search_ctx)
1553{
1554 ff_stack_T *sptr;
1555
1556 // clear up stack
1557 while ((sptr = ff_pop(search_ctx)) != NULL)
1558 ff_free_stack_element(sptr);
1559
1560 vim_free(search_ctx->ffsc_file_to_search);
1561 vim_free(search_ctx->ffsc_start_dir);
1562 vim_free(search_ctx->ffsc_fix_path);
1563#ifdef FEAT_PATH_EXTRA
1564 vim_free(search_ctx->ffsc_wc_path);
1565#endif
1566
1567#ifdef FEAT_PATH_EXTRA
1568 if (search_ctx->ffsc_stopdirs_v != NULL)
1569 {
1570 int i = 0;
1571
1572 while (search_ctx->ffsc_stopdirs_v[i] != NULL)
1573 {
1574 vim_free(search_ctx->ffsc_stopdirs_v[i]);
1575 i++;
1576 }
1577 vim_free(search_ctx->ffsc_stopdirs_v);
1578 }
1579 search_ctx->ffsc_stopdirs_v = NULL;
1580#endif
1581
1582 // reset everything
1583 search_ctx->ffsc_file_to_search = NULL;
1584 search_ctx->ffsc_start_dir = NULL;
1585 search_ctx->ffsc_fix_path = NULL;
1586#ifdef FEAT_PATH_EXTRA
1587 search_ctx->ffsc_wc_path = NULL;
1588 search_ctx->ffsc_level = 0;
1589#endif
1590}
1591
1592#ifdef FEAT_PATH_EXTRA
1593/*
1594 * check if the given path is in the stopdirs
1595 * returns TRUE if yes else FALSE
1596 */
1597 static int
1598ff_path_in_stoplist(char_u *path, int path_len, char_u **stopdirs_v)
1599{
1600 int i = 0;
1601
1602 // eat up trailing path separators, except the first
1603 while (path_len > 1 && vim_ispathsep(path[path_len - 1]))
1604 path_len--;
1605
1606 // if no path consider it as match
1607 if (path_len == 0)
1608 return TRUE;
1609
1610 for (i = 0; stopdirs_v[i] != NULL; i++)
1611 {
1612 if ((int)STRLEN(stopdirs_v[i]) > path_len)
1613 {
1614 // match for parent directory. So '/home' also matches
1615 // '/home/rks'. Check for PATHSEP in stopdirs_v[i], else
1616 // '/home/r' would also match '/home/rks'
1617 if (fnamencmp(stopdirs_v[i], path, path_len) == 0
1618 && vim_ispathsep(stopdirs_v[i][path_len]))
1619 return TRUE;
1620 }
1621 else
1622 {
1623 if (fnamecmp(stopdirs_v[i], path) == 0)
1624 return TRUE;
1625 }
1626 }
1627 return FALSE;
1628}
1629#endif
1630
1631#if defined(FEAT_SEARCHPATH) || defined(PROTO)
1632/*
1633 * Find the file name "ptr[len]" in the path. Also finds directory names.
1634 *
1635 * On the first call set the parameter 'first' to TRUE to initialize
1636 * the search. For repeating calls to FALSE.
1637 *
1638 * Repeating calls will return other files called 'ptr[len]' from the path.
1639 *
1640 * Only on the first call 'ptr' and 'len' are used. For repeating calls they
1641 * don't need valid values.
1642 *
1643 * If nothing found on the first call the option FNAME_MESS will issue the
1644 * message:
1645 * 'Can't find file "<file>" in path'
1646 * On repeating calls:
1647 * 'No more file "<file>" found in path'
1648 *
1649 * options:
1650 * FNAME_MESS give error message when not found
1651 *
1652 * Uses NameBuff[]!
1653 *
1654 * Returns an allocated string for the file name. NULL for error.
1655 *
1656 */
1657 char_u *
1658find_file_in_path(
1659 char_u *ptr, // file name
1660 int len, // length of file name
1661 int options,
1662 int first, // use count'th matching file name
1663 char_u *rel_fname) // file name searching relative to
1664{
1665 return find_file_in_path_option(ptr, len, options, first,
1666 *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path,
1667 FINDFILE_BOTH, rel_fname, curbuf->b_p_sua);
1668}
1669
1670static char_u *ff_file_to_find = NULL;
1671static void *fdip_search_ctx = NULL;
1672
1673# if defined(EXITFREE) || defined(PROTO)
1674 void
1675free_findfile(void)
1676{
1677 vim_free(ff_file_to_find);
1678 vim_findfile_cleanup(fdip_search_ctx);
1679 vim_free(ff_expand_buffer);
1680}
1681# endif
1682
1683/*
1684 * Find the directory name "ptr[len]" in the path.
1685 *
1686 * options:
1687 * FNAME_MESS give error message when not found
1688 * FNAME_UNESC unescape backslashes.
1689 *
1690 * Uses NameBuff[]!
1691 *
1692 * Returns an allocated string for the file name. NULL for error.
1693 */
1694 char_u *
1695find_directory_in_path(
1696 char_u *ptr, // file name
1697 int len, // length of file name
1698 int options,
1699 char_u *rel_fname) // file name searching relative to
1700{
1701 return find_file_in_path_option(ptr, len, options, TRUE, p_cdpath,
1702 FINDFILE_DIR, rel_fname, (char_u *)"");
1703}
1704
1705 char_u *
1706find_file_in_path_option(
1707 char_u *ptr, // file name
1708 int len, // length of file name
1709 int options,
1710 int first, // use count'th matching file name
1711 char_u *path_option, // p_path or p_cdpath
1712 int find_what, // FINDFILE_FILE, _DIR or _BOTH
1713 char_u *rel_fname, // file name we are looking relative to.
1714 char_u *suffixes) // list of suffixes, 'suffixesadd' option
1715{
1716 static char_u *dir;
1717 static int did_findfile_init = FALSE;
1718 char_u save_char;
1719 char_u *file_name = NULL;
1720 char_u *buf = NULL;
1721 int rel_to_curdir;
1722# ifdef AMIGA
1723 struct Process *proc = (struct Process *)FindTask(0L);
1724 APTR save_winptr = proc->pr_WindowPtr;
1725
1726 // Avoid a requester here for a volume that doesn't exist.
1727 proc->pr_WindowPtr = (APTR)-1L;
1728# endif
1729
1730 if (first == TRUE)
1731 {
1732 // copy file name into NameBuff, expanding environment variables
1733 save_char = ptr[len];
1734 ptr[len] = NUL;
1735 expand_env_esc(ptr, NameBuff, MAXPATHL, FALSE, TRUE, NULL);
1736 ptr[len] = save_char;
1737
1738 vim_free(ff_file_to_find);
1739 ff_file_to_find = vim_strsave(NameBuff);
1740 if (ff_file_to_find == NULL) // out of memory
1741 {
1742 file_name = NULL;
1743 goto theend;
1744 }
1745 if (options & FNAME_UNESC)
1746 {
1747 // Change all "\ " to " ".
1748 for (ptr = ff_file_to_find; *ptr != NUL; ++ptr)
1749 if (ptr[0] == '\\' && ptr[1] == ' ')
1750 mch_memmove(ptr, ptr + 1, STRLEN(ptr));
1751 }
1752 }
1753
1754 rel_to_curdir = (ff_file_to_find[0] == '.'
1755 && (ff_file_to_find[1] == NUL
1756 || vim_ispathsep(ff_file_to_find[1])
1757 || (ff_file_to_find[1] == '.'
1758 && (ff_file_to_find[2] == NUL
1759 || vim_ispathsep(ff_file_to_find[2])))));
1760 if (vim_isAbsName(ff_file_to_find)
1761 // "..", "../path", "." and "./path": don't use the path_option
1762 || rel_to_curdir
1763# if defined(MSWIN)
1764 // handle "\tmp" as absolute path
1765 || vim_ispathsep(ff_file_to_find[0])
1766 // handle "c:name" as absolute path
1767 || (ff_file_to_find[0] != NUL && ff_file_to_find[1] == ':')
1768# endif
1769# ifdef AMIGA
1770 // handle ":tmp" as absolute path
1771 || ff_file_to_find[0] == ':'
1772# endif
1773 )
1774 {
1775 /*
1776 * Absolute path, no need to use "path_option".
1777 * If this is not a first call, return NULL. We already returned a
1778 * filename on the first call.
1779 */
1780 if (first == TRUE)
1781 {
1782 int l;
1783 int run;
1784
1785 if (path_with_url(ff_file_to_find))
1786 {
1787 file_name = vim_strsave(ff_file_to_find);
1788 goto theend;
1789 }
1790
1791 // When FNAME_REL flag given first use the directory of the file.
1792 // Otherwise or when this fails use the current directory.
1793 for (run = 1; run <= 2; ++run)
1794 {
1795 l = (int)STRLEN(ff_file_to_find);
1796 if (run == 1
1797 && rel_to_curdir
1798 && (options & FNAME_REL)
1799 && rel_fname != NULL
1800 && STRLEN(rel_fname) + l < MAXPATHL)
1801 {
1802 STRCPY(NameBuff, rel_fname);
1803 STRCPY(gettail(NameBuff), ff_file_to_find);
1804 l = (int)STRLEN(NameBuff);
1805 }
1806 else
1807 {
1808 STRCPY(NameBuff, ff_file_to_find);
1809 run = 2;
1810 }
1811
1812 // When the file doesn't exist, try adding parts of
1813 // 'suffixesadd'.
1814 buf = suffixes;
1815 for (;;)
1816 {
1817 if (mch_getperm(NameBuff) >= 0
1818 && (find_what == FINDFILE_BOTH
1819 || ((find_what == FINDFILE_DIR)
1820 == mch_isdir(NameBuff))))
1821 {
1822 file_name = vim_strsave(NameBuff);
1823 goto theend;
1824 }
1825 if (*buf == NUL)
1826 break;
1827 copy_option_part(&buf, NameBuff + l, MAXPATHL - l, ",");
1828 }
1829 }
1830 }
1831 }
1832 else
1833 {
1834 /*
1835 * Loop over all paths in the 'path' or 'cdpath' option.
1836 * When "first" is set, first setup to the start of the option.
1837 * Otherwise continue to find the next match.
1838 */
1839 if (first == TRUE)
1840 {
1841 // vim_findfile_free_visited can handle a possible NULL pointer
1842 vim_findfile_free_visited(fdip_search_ctx);
1843 dir = path_option;
1844 did_findfile_init = FALSE;
1845 }
1846
1847 for (;;)
1848 {
1849 if (did_findfile_init)
1850 {
1851 file_name = vim_findfile(fdip_search_ctx);
1852 if (file_name != NULL)
1853 break;
1854
1855 did_findfile_init = FALSE;
1856 }
1857 else
1858 {
1859 char_u *r_ptr;
1860
1861 if (dir == NULL || *dir == NUL)
1862 {
1863 // We searched all paths of the option, now we can
1864 // free the search context.
1865 vim_findfile_cleanup(fdip_search_ctx);
1866 fdip_search_ctx = NULL;
1867 break;
1868 }
1869
Bram Moolenaar51e14382019-05-25 20:21:28 +02001870 if ((buf = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01001871 break;
1872
1873 // copy next path
1874 buf[0] = 0;
1875 copy_option_part(&dir, buf, MAXPATHL, " ,");
1876
1877# ifdef FEAT_PATH_EXTRA
1878 // get the stopdir string
1879 r_ptr = vim_findfile_stopdir(buf);
1880# else
1881 r_ptr = NULL;
1882# endif
1883 fdip_search_ctx = vim_findfile_init(buf, ff_file_to_find,
1884 r_ptr, 100, FALSE, find_what,
1885 fdip_search_ctx, FALSE, rel_fname);
1886 if (fdip_search_ctx != NULL)
1887 did_findfile_init = TRUE;
1888 vim_free(buf);
1889 }
1890 }
1891 }
1892 if (file_name == NULL && (options & FNAME_MESS))
1893 {
1894 if (first == TRUE)
1895 {
1896 if (find_what == FINDFILE_DIR)
1897 semsg(_("E344: Can't find directory \"%s\" in cdpath"),
1898 ff_file_to_find);
1899 else
1900 semsg(_("E345: Can't find file \"%s\" in path"),
1901 ff_file_to_find);
1902 }
1903 else
1904 {
1905 if (find_what == FINDFILE_DIR)
1906 semsg(_("E346: No more directory \"%s\" found in cdpath"),
1907 ff_file_to_find);
1908 else
1909 semsg(_("E347: No more file \"%s\" found in path"),
1910 ff_file_to_find);
1911 }
1912 }
1913
1914theend:
1915# ifdef AMIGA
1916 proc->pr_WindowPtr = save_winptr;
1917# endif
1918 return file_name;
1919}
1920
1921/*
1922 * Get the file name at the cursor.
1923 * If Visual mode is active, use the selected text if it's in one line.
1924 * Returns the name in allocated memory, NULL for failure.
1925 */
1926 char_u *
1927grab_file_name(long count, linenr_T *file_lnum)
1928{
1929 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
1930
1931 if (VIsual_active)
1932 {
1933 int len;
1934 char_u *ptr;
1935
1936 if (get_visual_text(NULL, &ptr, &len) == FAIL)
1937 return NULL;
1938 return find_file_name_in_path(ptr, len, options,
1939 count, curbuf->b_ffname);
1940 }
1941 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
1942}
1943
1944/*
1945 * Return the file name under or after the cursor.
1946 *
1947 * The 'path' option is searched if the file name is not absolute.
1948 * The string returned has been alloc'ed and should be freed by the caller.
1949 * NULL is returned if the file name or file is not found.
1950 *
1951 * options:
1952 * FNAME_MESS give error messages
1953 * FNAME_EXP expand to path
1954 * FNAME_HYP check for hypertext link
1955 * FNAME_INCL apply "includeexpr"
1956 */
1957 char_u *
1958file_name_at_cursor(int options, long count, linenr_T *file_lnum)
1959{
1960 return file_name_in_line(ml_get_curline(),
1961 curwin->w_cursor.col, options, count, curbuf->b_ffname,
1962 file_lnum);
1963}
1964
1965/*
1966 * Return the name of the file under or after ptr[col].
1967 * Otherwise like file_name_at_cursor().
1968 */
1969 char_u *
1970file_name_in_line(
1971 char_u *line,
1972 int col,
1973 int options,
1974 long count,
1975 char_u *rel_fname, // file we are searching relative to
1976 linenr_T *file_lnum) // line number after the file name
1977{
1978 char_u *ptr;
1979 int len;
1980 int in_type = TRUE;
1981 int is_url = FALSE;
1982
1983 /*
1984 * search forward for what could be the start of a file name
1985 */
1986 ptr = line + col;
1987 while (*ptr != NUL && !vim_isfilec(*ptr))
1988 MB_PTR_ADV(ptr);
1989 if (*ptr == NUL) // nothing found
1990 {
1991 if (options & FNAME_MESS)
1992 emsg(_("E446: No file name under cursor"));
1993 return NULL;
1994 }
1995
1996 /*
1997 * Search backward for first char of the file name.
1998 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
1999 */
2000 while (ptr > line)
2001 {
2002 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
2003 ptr -= len + 1;
2004 else if (vim_isfilec(ptr[-1])
2005 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
2006 --ptr;
2007 else
2008 break;
2009 }
2010
2011 /*
2012 * Search forward for the last char of the file name.
2013 * Also allow "://" when ':' is not in 'isfname'.
2014 */
2015 len = 0;
2016 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
2017 || ((options & FNAME_HYP) && path_is_url(ptr + len))
Bram Moolenaarcbef8e12019-03-09 12:32:56 +01002018 || (is_url && vim_strchr((char_u *)":?&=", ptr[len]) != NULL))
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002019 {
Bram Moolenaarcbef8e12019-03-09 12:32:56 +01002020 // After type:// we also include :, ?, & and = as valid characters, so that
2021 // http://google.com:8080?q=this&that=ok works.
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002022 if ((ptr[len] >= 'A' && ptr[len] <= 'Z') || (ptr[len] >= 'a' && ptr[len] <= 'z'))
2023 {
2024 if (in_type && path_is_url(ptr + len + 1))
2025 is_url = TRUE;
2026 }
2027 else
2028 in_type = FALSE;
2029
2030 if (ptr[len] == '\\')
2031 // Skip over the "\" in "\ ".
2032 ++len;
2033 if (has_mbyte)
2034 len += (*mb_ptr2len)(ptr + len);
2035 else
2036 ++len;
2037 }
2038
2039 /*
2040 * If there is trailing punctuation, remove it.
2041 * But don't remove "..", could be a directory name.
2042 */
2043 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
2044 && ptr[len - 2] != '.')
2045 --len;
2046
2047 if (file_lnum != NULL)
2048 {
2049 char_u *p;
2050
2051 // Get the number after the file name and a separator character
2052 p = ptr + len;
2053 p = skipwhite(p);
2054 if (*p != NUL)
2055 {
2056 if (!isdigit(*p))
2057 ++p; // skip the separator
2058 p = skipwhite(p);
2059 if (isdigit(*p))
2060 *file_lnum = (int)getdigits(&p);
2061 }
2062 }
2063
2064 return find_file_name_in_path(ptr, len, options, count, rel_fname);
2065}
2066
2067# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2068 static char_u *
2069eval_includeexpr(char_u *ptr, int len)
2070{
2071 char_u *res;
2072
2073 set_vim_var_string(VV_FNAME, ptr, len);
2074 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
2075 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
2076 set_vim_var_string(VV_FNAME, NULL, 0);
2077 return res;
2078}
2079# endif
2080
2081/*
2082 * Return the name of the file ptr[len] in 'path'.
2083 * Otherwise like file_name_at_cursor().
2084 */
2085 char_u *
2086find_file_name_in_path(
2087 char_u *ptr,
2088 int len,
2089 int options,
2090 long count,
2091 char_u *rel_fname) // file we are searching relative to
2092{
2093 char_u *file_name;
2094 int c;
2095# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2096 char_u *tofree = NULL;
2097
2098 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
2099 {
2100 tofree = eval_includeexpr(ptr, len);
2101 if (tofree != NULL)
2102 {
2103 ptr = tofree;
2104 len = (int)STRLEN(ptr);
2105 }
2106 }
2107# endif
2108
2109 if (options & FNAME_EXP)
2110 {
2111 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
2112 TRUE, rel_fname);
2113
2114# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2115 /*
2116 * If the file could not be found in a normal way, try applying
2117 * 'includeexpr' (unless done already).
2118 */
2119 if (file_name == NULL
2120 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
2121 {
2122 tofree = eval_includeexpr(ptr, len);
2123 if (tofree != NULL)
2124 {
2125 ptr = tofree;
2126 len = (int)STRLEN(ptr);
2127 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
2128 TRUE, rel_fname);
2129 }
2130 }
2131# endif
2132 if (file_name == NULL && (options & FNAME_MESS))
2133 {
2134 c = ptr[len];
2135 ptr[len] = NUL;
2136 semsg(_("E447: Can't find file \"%s\" in path"), ptr);
2137 ptr[len] = c;
2138 }
2139
2140 // Repeat finding the file "count" times. This matters when it
2141 // appears several times in the path.
2142 while (file_name != NULL && --count > 0)
2143 {
2144 vim_free(file_name);
2145 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
2146 }
2147 }
2148 else
2149 file_name = vim_strnsave(ptr, len);
2150
2151# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
2152 vim_free(tofree);
2153# endif
2154
2155 return file_name;
2156}
2157
2158/*
2159 * Return the end of the directory name, on the first path
2160 * separator:
2161 * "/path/file", "/path/dir/", "/path//dir", "/file"
2162 * ^ ^ ^ ^
2163 */
2164 static char_u *
2165gettail_dir(char_u *fname)
2166{
2167 char_u *dir_end = fname;
2168 char_u *next_dir_end = fname;
2169 int look_for_sep = TRUE;
2170 char_u *p;
2171
2172 for (p = fname; *p != NUL; )
2173 {
2174 if (vim_ispathsep(*p))
2175 {
2176 if (look_for_sep)
2177 {
2178 next_dir_end = p;
2179 look_for_sep = FALSE;
2180 }
2181 }
2182 else
2183 {
2184 if (!look_for_sep)
2185 dir_end = next_dir_end;
2186 look_for_sep = TRUE;
2187 }
2188 MB_PTR_ADV(p);
2189 }
2190 return dir_end;
2191}
2192
2193/*
2194 * return TRUE if 'c' is a path list separator.
2195 */
2196 int
2197vim_ispathlistsep(int c)
2198{
2199# ifdef UNIX
2200 return (c == ':');
2201# else
2202 return (c == ';'); // might not be right for every system...
2203# endif
2204}
2205
2206/*
2207 * Moves "*psep" back to the previous path separator in "path".
2208 * Returns FAIL is "*psep" ends up at the beginning of "path".
2209 */
2210 static int
2211find_previous_pathsep(char_u *path, char_u **psep)
2212{
2213 // skip the current separator
2214 if (*psep > path && vim_ispathsep(**psep))
2215 --*psep;
2216
2217 // find the previous separator
2218 while (*psep > path)
2219 {
2220 if (vim_ispathsep(**psep))
2221 return OK;
2222 MB_PTR_BACK(path, *psep);
2223 }
2224
2225 return FAIL;
2226}
2227
2228/*
2229 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
2230 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
2231 */
2232 static int
2233is_unique(char_u *maybe_unique, garray_T *gap, int i)
2234{
2235 int j;
2236 int candidate_len;
2237 int other_path_len;
2238 char_u **other_paths = (char_u **)gap->ga_data;
2239 char_u *rival;
2240
2241 for (j = 0; j < gap->ga_len; j++)
2242 {
2243 if (j == i)
2244 continue; // don't compare it with itself
2245
2246 candidate_len = (int)STRLEN(maybe_unique);
2247 other_path_len = (int)STRLEN(other_paths[j]);
2248 if (other_path_len < candidate_len)
2249 continue; // it's different when it's shorter
2250
2251 rival = other_paths[j] + other_path_len - candidate_len;
2252 if (fnamecmp(maybe_unique, rival) == 0
2253 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
2254 return FALSE; // match
2255 }
2256
2257 return TRUE; // no match found
2258}
2259
2260/*
2261 * Split the 'path' option into an array of strings in garray_T. Relative
2262 * paths are expanded to their equivalent fullpath. This includes the "."
2263 * (relative to current buffer directory) and empty path (relative to current
2264 * directory) notations.
2265 *
2266 * TODO: handle upward search (;) and path limiter (**N) notations by
2267 * expanding each into their equivalent path(s).
2268 */
2269 static void
2270expand_path_option(char_u *curdir, garray_T *gap)
2271{
2272 char_u *path_option = *curbuf->b_p_path == NUL
2273 ? p_path : curbuf->b_p_path;
2274 char_u *buf;
2275 char_u *p;
2276 int len;
2277
Bram Moolenaar51e14382019-05-25 20:21:28 +02002278 if ((buf = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002279 return;
2280
2281 while (*path_option != NUL)
2282 {
2283 copy_option_part(&path_option, buf, MAXPATHL, " ,");
2284
2285 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
2286 {
2287 // Relative to current buffer:
2288 // "/path/file" + "." -> "/path/"
2289 // "/path/file" + "./subdir" -> "/path/subdir"
2290 if (curbuf->b_ffname == NULL)
2291 continue;
2292 p = gettail(curbuf->b_ffname);
2293 len = (int)(p - curbuf->b_ffname);
2294 if (len + (int)STRLEN(buf) >= MAXPATHL)
2295 continue;
2296 if (buf[1] == NUL)
2297 buf[len] = NUL;
2298 else
2299 STRMOVE(buf + len, buf + 2);
2300 mch_memmove(buf, curbuf->b_ffname, len);
2301 simplify_filename(buf);
2302 }
2303 else if (buf[0] == NUL)
2304 // relative to current directory
2305 STRCPY(buf, curdir);
2306 else if (path_with_url(buf))
2307 // URL can't be used here
2308 continue;
2309 else if (!mch_isFullName(buf))
2310 {
2311 // Expand relative path to their full path equivalent
2312 len = (int)STRLEN(curdir);
2313 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
2314 continue;
2315 STRMOVE(buf + len + 1, buf);
2316 STRCPY(buf, curdir);
2317 buf[len] = PATHSEP;
2318 simplify_filename(buf);
2319 }
2320
2321 if (ga_grow(gap, 1) == FAIL)
2322 break;
2323
2324# if defined(MSWIN)
2325 // Avoid the path ending in a backslash, it fails when a comma is
2326 // appended.
2327 len = (int)STRLEN(buf);
2328 if (buf[len - 1] == '\\')
2329 buf[len - 1] = '/';
2330# endif
2331
2332 p = vim_strsave(buf);
2333 if (p == NULL)
2334 break;
2335 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
2336 }
2337
2338 vim_free(buf);
2339}
2340
2341/*
2342 * Returns a pointer to the file or directory name in "fname" that matches the
2343 * longest path in "ga"p, or NULL if there is no match. For example:
2344 *
2345 * path: /foo/bar/baz
2346 * fname: /foo/bar/baz/quux.txt
2347 * returns: ^this
2348 */
2349 static char_u *
2350get_path_cutoff(char_u *fname, garray_T *gap)
2351{
2352 int i;
2353 int maxlen = 0;
2354 char_u **path_part = (char_u **)gap->ga_data;
2355 char_u *cutoff = NULL;
2356
2357 for (i = 0; i < gap->ga_len; i++)
2358 {
2359 int j = 0;
2360
2361 while ((fname[j] == path_part[i][j]
2362# if defined(MSWIN)
2363 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
2364# endif
2365 ) && fname[j] != NUL && path_part[i][j] != NUL)
2366 j++;
2367 if (j > maxlen)
2368 {
2369 maxlen = j;
2370 cutoff = &fname[j];
2371 }
2372 }
2373
2374 // skip to the file or directory name
2375 if (cutoff != NULL)
2376 while (vim_ispathsep(*cutoff))
2377 MB_PTR_ADV(cutoff);
2378
2379 return cutoff;
2380}
2381
2382/*
2383 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
2384 * that they are unique with respect to each other while conserving the part
2385 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
2386 */
2387 void
2388uniquefy_paths(garray_T *gap, char_u *pattern)
2389{
2390 int i;
2391 int len;
2392 char_u **fnames = (char_u **)gap->ga_data;
2393 int sort_again = FALSE;
2394 char_u *pat;
2395 char_u *file_pattern;
2396 char_u *curdir;
2397 regmatch_T regmatch;
2398 garray_T path_ga;
2399 char_u **in_curdir = NULL;
2400 char_u *short_name;
2401
2402 remove_duplicates(gap);
2403 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
2404
2405 /*
2406 * We need to prepend a '*' at the beginning of file_pattern so that the
2407 * regex matches anywhere in the path. FIXME: is this valid for all
2408 * possible patterns?
2409 */
2410 len = (int)STRLEN(pattern);
2411 file_pattern = alloc(len + 2);
2412 if (file_pattern == NULL)
2413 return;
2414 file_pattern[0] = '*';
2415 file_pattern[1] = NUL;
2416 STRCAT(file_pattern, pattern);
2417 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
2418 vim_free(file_pattern);
2419 if (pat == NULL)
2420 return;
2421
2422 regmatch.rm_ic = TRUE; // always ignore case
2423 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2424 vim_free(pat);
2425 if (regmatch.regprog == NULL)
2426 return;
2427
Bram Moolenaar51e14382019-05-25 20:21:28 +02002428 if ((curdir = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002429 goto theend;
2430 mch_dirname(curdir, MAXPATHL);
2431 expand_path_option(curdir, &path_ga);
2432
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002433 in_curdir = ALLOC_CLEAR_MULT(char_u *, gap->ga_len);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002434 if (in_curdir == NULL)
2435 goto theend;
2436
2437 for (i = 0; i < gap->ga_len && !got_int; i++)
2438 {
2439 char_u *path = fnames[i];
2440 int is_in_curdir;
2441 char_u *dir_end = gettail_dir(path);
2442 char_u *pathsep_p;
2443 char_u *path_cutoff;
2444
2445 len = (int)STRLEN(path);
2446 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
2447 && curdir[dir_end - path] == NUL;
2448 if (is_in_curdir)
2449 in_curdir[i] = vim_strsave(path);
2450
2451 // Shorten the filename while maintaining its uniqueness
2452 path_cutoff = get_path_cutoff(path, &path_ga);
2453
2454 // Don't assume all files can be reached without path when search
2455 // pattern starts with star star slash, so only remove path_cutoff
2456 // when possible.
2457 if (pattern[0] == '*' && pattern[1] == '*'
2458 && vim_ispathsep_nocolon(pattern[2])
2459 && path_cutoff != NULL
2460 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
2461 && is_unique(path_cutoff, gap, i))
2462 {
2463 sort_again = TRUE;
2464 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
2465 }
2466 else
2467 {
2468 // Here all files can be reached without path, so get shortest
2469 // unique path. We start at the end of the path.
2470 pathsep_p = path + len - 1;
2471
2472 while (find_previous_pathsep(path, &pathsep_p))
2473 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
2474 && is_unique(pathsep_p + 1, gap, i)
2475 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
2476 {
2477 sort_again = TRUE;
2478 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
2479 break;
2480 }
2481 }
2482
2483 if (mch_isFullName(path))
2484 {
2485 /*
2486 * Last resort: shorten relative to curdir if possible.
2487 * 'possible' means:
2488 * 1. It is under the current directory.
2489 * 2. The result is actually shorter than the original.
2490 *
2491 * Before curdir After
2492 * /foo/bar/file.txt /foo/bar ./file.txt
2493 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
2494 * /file.txt / /file.txt
2495 * c:\file.txt c:\ .\file.txt
2496 */
2497 short_name = shorten_fname(path, curdir);
2498 if (short_name != NULL && short_name > path + 1
2499# if defined(MSWIN)
2500 // On windows,
2501 // shorten_fname("c:\a\a.txt", "c:\a\b")
2502 // returns "\a\a.txt", which is not really the short
2503 // name, hence:
2504 && !vim_ispathsep(*short_name)
2505# endif
2506 )
2507 {
2508 STRCPY(path, ".");
2509 add_pathsep(path);
2510 STRMOVE(path + STRLEN(path), short_name);
2511 }
2512 }
2513 ui_breakcheck();
2514 }
2515
2516 // Shorten filenames in /in/current/directory/{filename}
2517 for (i = 0; i < gap->ga_len && !got_int; i++)
2518 {
2519 char_u *rel_path;
2520 char_u *path = in_curdir[i];
2521
2522 if (path == NULL)
2523 continue;
2524
2525 // If the {filename} is not unique, change it to ./{filename}.
2526 // Else reduce it to {filename}
2527 short_name = shorten_fname(path, curdir);
2528 if (short_name == NULL)
2529 short_name = path;
2530 if (is_unique(short_name, gap, i))
2531 {
2532 STRCPY(fnames[i], short_name);
2533 continue;
2534 }
2535
Bram Moolenaar51e14382019-05-25 20:21:28 +02002536 rel_path = alloc(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2);
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002537 if (rel_path == NULL)
2538 goto theend;
2539 STRCPY(rel_path, ".");
2540 add_pathsep(rel_path);
2541 STRCAT(rel_path, short_name);
2542
2543 vim_free(fnames[i]);
2544 fnames[i] = rel_path;
2545 sort_again = TRUE;
2546 ui_breakcheck();
2547 }
2548
2549theend:
2550 vim_free(curdir);
2551 if (in_curdir != NULL)
2552 {
2553 for (i = 0; i < gap->ga_len; i++)
2554 vim_free(in_curdir[i]);
2555 vim_free(in_curdir);
2556 }
2557 ga_clear_strings(&path_ga);
2558 vim_regfree(regmatch.regprog);
2559
2560 if (sort_again)
2561 remove_duplicates(gap);
2562}
2563
2564/*
2565 * Calls globpath() with 'path' values for the given pattern and stores the
2566 * result in "gap".
2567 * Returns the total number of matches.
2568 */
2569 int
2570expand_in_path(
2571 garray_T *gap,
2572 char_u *pattern,
2573 int flags) // EW_* flags
2574{
2575 char_u *curdir;
2576 garray_T path_ga;
2577 char_u *paths = NULL;
2578 int glob_flags = 0;
2579
Bram Moolenaar964b3742019-05-24 18:54:09 +02002580 if ((curdir = alloc(MAXPATHL)) == NULL)
Bram Moolenaar5fd0f502019-02-13 23:13:28 +01002581 return 0;
2582 mch_dirname(curdir, MAXPATHL);
2583
2584 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
2585 expand_path_option(curdir, &path_ga);
2586 vim_free(curdir);
2587 if (path_ga.ga_len == 0)
2588 return 0;
2589
2590 paths = ga_concat_strings(&path_ga, ",");
2591 ga_clear_strings(&path_ga);
2592 if (paths == NULL)
2593 return 0;
2594
2595 if (flags & EW_ICASE)
2596 glob_flags |= WILD_ICASE;
2597 if (flags & EW_ADDSLASH)
2598 glob_flags |= WILD_ADD_SLASH;
2599 globpath(paths, pattern, gap, glob_flags);
2600 vim_free(paths);
2601
2602 return gap->ga_len;
2603}
2604
2605#endif // FEAT_SEARCHPATH
Bram Moolenaarb4a60202019-03-31 19:40:07 +02002606
2607/*
2608 * Converts a file name into a canonical form. It simplifies a file name into
2609 * its simplest form by stripping out unneeded components, if any. The
2610 * resulting file name is simplified in place and will either be the same
2611 * length as that supplied, or shorter.
2612 */
2613 void
2614simplify_filename(char_u *filename)
2615{
2616#ifndef AMIGA // Amiga doesn't have "..", it uses "/"
2617 int components = 0;
2618 char_u *p, *tail, *start;
2619 int stripping_disabled = FALSE;
2620 int relative = TRUE;
2621
2622 p = filename;
2623# ifdef BACKSLASH_IN_FILENAME
2624 if (p[1] == ':') // skip "x:"
2625 p += 2;
2626# endif
2627
2628 if (vim_ispathsep(*p))
2629 {
2630 relative = FALSE;
2631 do
2632 ++p;
2633 while (vim_ispathsep(*p));
2634 }
2635 start = p; // remember start after "c:/" or "/" or "///"
2636
2637 do
2638 {
2639 // At this point "p" is pointing to the char following a single "/"
2640 // or "p" is at the "start" of the (absolute or relative) path name.
2641# ifdef VMS
2642 // VMS allows device:[path] - don't strip the [ in directory
2643 if ((*p == '[' || *p == '<') && p > filename && p[-1] == ':')
2644 {
2645 // :[ or :< composition: vms directory component
2646 ++components;
2647 p = getnextcomp(p + 1);
2648 }
2649 // allow remote calls as host"user passwd"::device:[path]
2650 else if (p[0] == ':' && p[1] == ':' && p > filename && p[-1] == '"' )
2651 {
2652 // ":: composition: vms host/passwd component
2653 ++components;
2654 p = getnextcomp(p + 2);
2655 }
2656 else
2657# endif
2658 if (vim_ispathsep(*p))
2659 STRMOVE(p, p + 1); // remove duplicate "/"
2660 else if (p[0] == '.' && (vim_ispathsep(p[1]) || p[1] == NUL))
2661 {
2662 if (p == start && relative)
2663 p += 1 + (p[1] != NUL); // keep single "." or leading "./"
2664 else
2665 {
2666 // Strip "./" or ".///". If we are at the end of the file name
2667 // and there is no trailing path separator, either strip "/." if
2668 // we are after "start", or strip "." if we are at the beginning
2669 // of an absolute path name .
2670 tail = p + 1;
2671 if (p[1] != NUL)
2672 while (vim_ispathsep(*tail))
2673 MB_PTR_ADV(tail);
2674 else if (p > start)
2675 --p; // strip preceding path separator
2676 STRMOVE(p, tail);
2677 }
2678 }
2679 else if (p[0] == '.' && p[1] == '.' &&
2680 (vim_ispathsep(p[2]) || p[2] == NUL))
2681 {
2682 // Skip to after ".." or "../" or "..///".
2683 tail = p + 2;
2684 while (vim_ispathsep(*tail))
2685 MB_PTR_ADV(tail);
2686
2687 if (components > 0) // strip one preceding component
2688 {
2689 int do_strip = FALSE;
2690 char_u saved_char;
2691 stat_T st;
2692
2693 /* Don't strip for an erroneous file name. */
2694 if (!stripping_disabled)
2695 {
2696 // If the preceding component does not exist in the file
2697 // system, we strip it. On Unix, we don't accept a symbolic
2698 // link that refers to a non-existent file.
2699 saved_char = p[-1];
2700 p[-1] = NUL;
2701# ifdef UNIX
2702 if (mch_lstat((char *)filename, &st) < 0)
2703# else
2704 if (mch_stat((char *)filename, &st) < 0)
2705# endif
2706 do_strip = TRUE;
2707 p[-1] = saved_char;
2708
2709 --p;
2710 // Skip back to after previous '/'.
2711 while (p > start && !after_pathsep(start, p))
2712 MB_PTR_BACK(start, p);
2713
2714 if (!do_strip)
2715 {
2716 // If the component exists in the file system, check
2717 // that stripping it won't change the meaning of the
2718 // file name. First get information about the
2719 // unstripped file name. This may fail if the component
2720 // to strip is not a searchable directory (but a regular
2721 // file, for instance), since the trailing "/.." cannot
2722 // be applied then. We don't strip it then since we
2723 // don't want to replace an erroneous file name by
2724 // a valid one, and we disable stripping of later
2725 // components.
2726 saved_char = *tail;
2727 *tail = NUL;
2728 if (mch_stat((char *)filename, &st) >= 0)
2729 do_strip = TRUE;
2730 else
2731 stripping_disabled = TRUE;
2732 *tail = saved_char;
2733# ifdef UNIX
2734 if (do_strip)
2735 {
2736 stat_T new_st;
2737
2738 // On Unix, the check for the unstripped file name
2739 // above works also for a symbolic link pointing to
2740 // a searchable directory. But then the parent of
2741 // the directory pointed to by the link must be the
2742 // same as the stripped file name. (The latter
2743 // exists in the file system since it is the
2744 // component's parent directory.)
2745 if (p == start && relative)
2746 (void)mch_stat(".", &new_st);
2747 else
2748 {
2749 saved_char = *p;
2750 *p = NUL;
2751 (void)mch_stat((char *)filename, &new_st);
2752 *p = saved_char;
2753 }
2754
2755 if (new_st.st_ino != st.st_ino ||
2756 new_st.st_dev != st.st_dev)
2757 {
2758 do_strip = FALSE;
2759 // We don't disable stripping of later
2760 // components since the unstripped path name is
2761 // still valid.
2762 }
2763 }
2764# endif
2765 }
2766 }
2767
2768 if (!do_strip)
2769 {
2770 // Skip the ".." or "../" and reset the counter for the
2771 // components that might be stripped later on.
2772 p = tail;
2773 components = 0;
2774 }
2775 else
2776 {
2777 // Strip previous component. If the result would get empty
2778 // and there is no trailing path separator, leave a single
2779 // "." instead. If we are at the end of the file name and
2780 // there is no trailing path separator and a preceding
2781 // component is left after stripping, strip its trailing
2782 // path separator as well.
2783 if (p == start && relative && tail[-1] == '.')
2784 {
2785 *p++ = '.';
2786 *p = NUL;
2787 }
2788 else
2789 {
2790 if (p > start && tail[-1] == '.')
2791 --p;
2792 STRMOVE(p, tail); // strip previous component
2793 }
2794
2795 --components;
2796 }
2797 }
2798 else if (p == start && !relative) // leading "/.." or "/../"
2799 STRMOVE(p, tail); // strip ".." or "../"
2800 else
2801 {
2802 if (p == start + 2 && p[-2] == '.') // leading "./../"
2803 {
2804 STRMOVE(p - 2, p); // strip leading "./"
2805 tail -= 2;
2806 }
2807 p = tail; // skip to char after ".." or "../"
2808 }
2809 }
2810 else
2811 {
2812 ++components; // simple path component
2813 p = getnextcomp(p);
2814 }
2815 } while (*p != NUL);
2816#endif // !AMIGA
2817}