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